FunctionLoweringInfo.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. //===-- FunctionLoweringInfo.cpp ------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This implements routines for translating functions from LLVM IR into
  11. // Machine IR.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #define DEBUG_TYPE "function-lowering-info"
  15. #include "FunctionLoweringInfo.h"
  16. #include "llvm/CallingConv.h"
  17. #include "llvm/DerivedTypes.h"
  18. #include "llvm/Function.h"
  19. #include "llvm/Instructions.h"
  20. #include "llvm/IntrinsicInst.h"
  21. #include "llvm/LLVMContext.h"
  22. #include "llvm/Module.h"
  23. #include "llvm/CodeGen/MachineFunction.h"
  24. #include "llvm/CodeGen/MachineFrameInfo.h"
  25. #include "llvm/CodeGen/MachineInstrBuilder.h"
  26. #include "llvm/CodeGen/MachineModuleInfo.h"
  27. #include "llvm/CodeGen/MachineRegisterInfo.h"
  28. #include "llvm/Analysis/DebugInfo.h"
  29. #include "llvm/Target/TargetRegisterInfo.h"
  30. #include "llvm/Target/TargetData.h"
  31. #include "llvm/Target/TargetFrameInfo.h"
  32. #include "llvm/Target/TargetInstrInfo.h"
  33. #include "llvm/Target/TargetIntrinsicInfo.h"
  34. #include "llvm/Target/TargetLowering.h"
  35. #include "llvm/Target/TargetOptions.h"
  36. #include "llvm/Support/Compiler.h"
  37. #include "llvm/Support/Debug.h"
  38. #include "llvm/Support/ErrorHandling.h"
  39. #include "llvm/Support/MathExtras.h"
  40. #include "llvm/Support/raw_ostream.h"
  41. #include <algorithm>
  42. using namespace llvm;
  43. /// ComputeLinearIndex - Given an LLVM IR aggregate type and a sequence
  44. /// of insertvalue or extractvalue indices that identify a member, return
  45. /// the linearized index of the start of the member.
  46. ///
  47. unsigned llvm::ComputeLinearIndex(const TargetLowering &TLI, const Type *Ty,
  48. const unsigned *Indices,
  49. const unsigned *IndicesEnd,
  50. unsigned CurIndex) {
  51. // Base case: We're done.
  52. if (Indices && Indices == IndicesEnd)
  53. return CurIndex;
  54. // Given a struct type, recursively traverse the elements.
  55. if (const StructType *STy = dyn_cast<StructType>(Ty)) {
  56. for (StructType::element_iterator EB = STy->element_begin(),
  57. EI = EB,
  58. EE = STy->element_end();
  59. EI != EE; ++EI) {
  60. if (Indices && *Indices == unsigned(EI - EB))
  61. return ComputeLinearIndex(TLI, *EI, Indices+1, IndicesEnd, CurIndex);
  62. CurIndex = ComputeLinearIndex(TLI, *EI, 0, 0, CurIndex);
  63. }
  64. return CurIndex;
  65. }
  66. // Given an array type, recursively traverse the elements.
  67. else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
  68. const Type *EltTy = ATy->getElementType();
  69. for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) {
  70. if (Indices && *Indices == i)
  71. return ComputeLinearIndex(TLI, EltTy, Indices+1, IndicesEnd, CurIndex);
  72. CurIndex = ComputeLinearIndex(TLI, EltTy, 0, 0, CurIndex);
  73. }
  74. return CurIndex;
  75. }
  76. // We haven't found the type we're looking for, so keep searching.
  77. return CurIndex + 1;
  78. }
  79. /// ComputeValueVTs - Given an LLVM IR type, compute a sequence of
  80. /// EVTs that represent all the individual underlying
  81. /// non-aggregate types that comprise it.
  82. ///
  83. /// If Offsets is non-null, it points to a vector to be filled in
  84. /// with the in-memory offsets of each of the individual values.
  85. ///
  86. void llvm::ComputeValueVTs(const TargetLowering &TLI, const Type *Ty,
  87. SmallVectorImpl<EVT> &ValueVTs,
  88. SmallVectorImpl<uint64_t> *Offsets,
  89. uint64_t StartingOffset) {
  90. // Given a struct type, recursively traverse the elements.
  91. if (const StructType *STy = dyn_cast<StructType>(Ty)) {
  92. const StructLayout *SL = TLI.getTargetData()->getStructLayout(STy);
  93. for (StructType::element_iterator EB = STy->element_begin(),
  94. EI = EB,
  95. EE = STy->element_end();
  96. EI != EE; ++EI)
  97. ComputeValueVTs(TLI, *EI, ValueVTs, Offsets,
  98. StartingOffset + SL->getElementOffset(EI - EB));
  99. return;
  100. }
  101. // Given an array type, recursively traverse the elements.
  102. if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
  103. const Type *EltTy = ATy->getElementType();
  104. uint64_t EltSize = TLI.getTargetData()->getTypeAllocSize(EltTy);
  105. for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
  106. ComputeValueVTs(TLI, EltTy, ValueVTs, Offsets,
  107. StartingOffset + i * EltSize);
  108. return;
  109. }
  110. // Interpret void as zero return values.
  111. if (Ty->isVoidTy())
  112. return;
  113. // Base case: we can get an EVT for this LLVM IR type.
  114. ValueVTs.push_back(TLI.getValueType(Ty));
  115. if (Offsets)
  116. Offsets->push_back(StartingOffset);
  117. }
  118. /// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by
  119. /// PHI nodes or outside of the basic block that defines it, or used by a
  120. /// switch or atomic instruction, which may expand to multiple basic blocks.
  121. static bool isUsedOutsideOfDefiningBlock(Instruction *I) {
  122. if (isa<PHINode>(I)) return true;
  123. BasicBlock *BB = I->getParent();
  124. for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; ++UI)
  125. if (cast<Instruction>(*UI)->getParent() != BB || isa<PHINode>(*UI))
  126. return true;
  127. return false;
  128. }
  129. /// isOnlyUsedInEntryBlock - If the specified argument is only used in the
  130. /// entry block, return true. This includes arguments used by switches, since
  131. /// the switch may expand into multiple basic blocks.
  132. static bool isOnlyUsedInEntryBlock(Argument *A, bool EnableFastISel) {
  133. // With FastISel active, we may be splitting blocks, so force creation
  134. // of virtual registers for all non-dead arguments.
  135. // Don't force virtual registers for byval arguments though, because
  136. // fast-isel can't handle those in all cases.
  137. if (EnableFastISel && !A->hasByValAttr())
  138. return A->use_empty();
  139. BasicBlock *Entry = A->getParent()->begin();
  140. for (Value::use_iterator UI = A->use_begin(), E = A->use_end(); UI != E; ++UI)
  141. if (cast<Instruction>(*UI)->getParent() != Entry || isa<SwitchInst>(*UI))
  142. return false; // Use not in entry block.
  143. return true;
  144. }
  145. FunctionLoweringInfo::FunctionLoweringInfo(TargetLowering &tli)
  146. : TLI(tli) {
  147. }
  148. void FunctionLoweringInfo::set(Function &fn, MachineFunction &mf,
  149. bool EnableFastISel) {
  150. Fn = &fn;
  151. MF = &mf;
  152. RegInfo = &MF->getRegInfo();
  153. // Create a vreg for each argument register that is not dead and is used
  154. // outside of the entry block for the function.
  155. for (Function::arg_iterator AI = Fn->arg_begin(), E = Fn->arg_end();
  156. AI != E; ++AI)
  157. if (!isOnlyUsedInEntryBlock(AI, EnableFastISel))
  158. InitializeRegForValue(AI);
  159. // Initialize the mapping of values to registers. This is only set up for
  160. // instruction values that are used outside of the block that defines
  161. // them.
  162. Function::iterator BB = Fn->begin(), EB = Fn->end();
  163. for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
  164. if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
  165. if (ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) {
  166. const Type *Ty = AI->getAllocatedType();
  167. uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
  168. unsigned Align =
  169. std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
  170. AI->getAlignment());
  171. TySize *= CUI->getZExtValue(); // Get total allocated size.
  172. if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects.
  173. StaticAllocaMap[AI] =
  174. MF->getFrameInfo()->CreateStackObject(TySize, Align, false);
  175. }
  176. for (; BB != EB; ++BB)
  177. for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
  178. if (!I->use_empty() && isUsedOutsideOfDefiningBlock(I))
  179. if (!isa<AllocaInst>(I) ||
  180. !StaticAllocaMap.count(cast<AllocaInst>(I)))
  181. InitializeRegForValue(I);
  182. // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This
  183. // also creates the initial PHI MachineInstrs, though none of the input
  184. // operands are populated.
  185. for (BB = Fn->begin(), EB = Fn->end(); BB != EB; ++BB) {
  186. MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB);
  187. MBBMap[BB] = MBB;
  188. MF->push_back(MBB);
  189. // Transfer the address-taken flag. This is necessary because there could
  190. // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only
  191. // the first one should be marked.
  192. if (BB->hasAddressTaken())
  193. MBB->setHasAddressTaken();
  194. // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
  195. // appropriate.
  196. PHINode *PN;
  197. DebugLoc DL;
  198. for (BasicBlock::iterator
  199. I = BB->begin(), E = BB->end(); I != E; ++I) {
  200. PN = dyn_cast<PHINode>(I);
  201. if (!PN || PN->use_empty()) continue;
  202. unsigned PHIReg = ValueMap[PN];
  203. assert(PHIReg && "PHI node does not have an assigned virtual register!");
  204. SmallVector<EVT, 4> ValueVTs;
  205. ComputeValueVTs(TLI, PN->getType(), ValueVTs);
  206. for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
  207. EVT VT = ValueVTs[vti];
  208. unsigned NumRegisters = TLI.getNumRegisters(Fn->getContext(), VT);
  209. const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
  210. for (unsigned i = 0; i != NumRegisters; ++i)
  211. BuildMI(MBB, DL, TII->get(TargetOpcode::PHI), PHIReg + i);
  212. PHIReg += NumRegisters;
  213. }
  214. }
  215. }
  216. }
  217. /// clear - Clear out all the function-specific state. This returns this
  218. /// FunctionLoweringInfo to an empty state, ready to be used for a
  219. /// different function.
  220. void FunctionLoweringInfo::clear() {
  221. MBBMap.clear();
  222. ValueMap.clear();
  223. StaticAllocaMap.clear();
  224. #ifndef NDEBUG
  225. CatchInfoLost.clear();
  226. CatchInfoFound.clear();
  227. #endif
  228. LiveOutRegInfo.clear();
  229. }
  230. unsigned FunctionLoweringInfo::MakeReg(EVT VT) {
  231. return RegInfo->createVirtualRegister(TLI.getRegClassFor(VT));
  232. }
  233. /// CreateRegForValue - Allocate the appropriate number of virtual registers of
  234. /// the correctly promoted or expanded types. Assign these registers
  235. /// consecutive vreg numbers and return the first assigned number.
  236. ///
  237. /// In the case that the given value has struct or array type, this function
  238. /// will assign registers for each member or element.
  239. ///
  240. unsigned FunctionLoweringInfo::CreateRegForValue(const Value *V) {
  241. SmallVector<EVT, 4> ValueVTs;
  242. ComputeValueVTs(TLI, V->getType(), ValueVTs);
  243. unsigned FirstReg = 0;
  244. for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
  245. EVT ValueVT = ValueVTs[Value];
  246. EVT RegisterVT = TLI.getRegisterType(V->getContext(), ValueVT);
  247. unsigned NumRegs = TLI.getNumRegisters(V->getContext(), ValueVT);
  248. for (unsigned i = 0; i != NumRegs; ++i) {
  249. unsigned R = MakeReg(RegisterVT);
  250. if (!FirstReg) FirstReg = R;
  251. }
  252. }
  253. return FirstReg;
  254. }
  255. /// ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V.
  256. GlobalVariable *llvm::ExtractTypeInfo(Value *V) {
  257. V = V->stripPointerCasts();
  258. GlobalVariable *GV = dyn_cast<GlobalVariable>(V);
  259. assert ((GV || isa<ConstantPointerNull>(V)) &&
  260. "TypeInfo must be a global variable or NULL");
  261. return GV;
  262. }
  263. /// AddCatchInfo - Extract the personality and type infos from an eh.selector
  264. /// call, and add them to the specified machine basic block.
  265. void llvm::AddCatchInfo(CallInst &I, MachineModuleInfo *MMI,
  266. MachineBasicBlock *MBB) {
  267. // Inform the MachineModuleInfo of the personality for this landing pad.
  268. ConstantExpr *CE = cast<ConstantExpr>(I.getOperand(2));
  269. assert(CE->getOpcode() == Instruction::BitCast &&
  270. isa<Function>(CE->getOperand(0)) &&
  271. "Personality should be a function");
  272. MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0)));
  273. // Gather all the type infos for this landing pad and pass them along to
  274. // MachineModuleInfo.
  275. std::vector<GlobalVariable *> TyInfo;
  276. unsigned N = I.getNumOperands();
  277. for (unsigned i = N - 1; i > 2; --i) {
  278. if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(i))) {
  279. unsigned FilterLength = CI->getZExtValue();
  280. unsigned FirstCatch = i + FilterLength + !FilterLength;
  281. assert (FirstCatch <= N && "Invalid filter length");
  282. if (FirstCatch < N) {
  283. TyInfo.reserve(N - FirstCatch);
  284. for (unsigned j = FirstCatch; j < N; ++j)
  285. TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
  286. MMI->addCatchTypeInfo(MBB, TyInfo);
  287. TyInfo.clear();
  288. }
  289. if (!FilterLength) {
  290. // Cleanup.
  291. MMI->addCleanup(MBB);
  292. } else {
  293. // Filter.
  294. TyInfo.reserve(FilterLength - 1);
  295. for (unsigned j = i + 1; j < FirstCatch; ++j)
  296. TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
  297. MMI->addFilterTypeInfo(MBB, TyInfo);
  298. TyInfo.clear();
  299. }
  300. N = i;
  301. }
  302. }
  303. if (N > 3) {
  304. TyInfo.reserve(N - 3);
  305. for (unsigned j = 3; j < N; ++j)
  306. TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
  307. MMI->addCatchTypeInfo(MBB, TyInfo);
  308. }
  309. }
  310. void llvm::CopyCatchInfo(BasicBlock *SrcBB, BasicBlock *DestBB,
  311. MachineModuleInfo *MMI, FunctionLoweringInfo &FLI) {
  312. for (BasicBlock::iterator I = SrcBB->begin(), E = --SrcBB->end(); I != E; ++I)
  313. if (EHSelectorInst *EHSel = dyn_cast<EHSelectorInst>(I)) {
  314. // Apply the catch info to DestBB.
  315. AddCatchInfo(*EHSel, MMI, FLI.MBBMap[DestBB]);
  316. #ifndef NDEBUG
  317. if (!FLI.MBBMap[SrcBB]->isLandingPad())
  318. FLI.CatchInfoFound.insert(EHSel);
  319. #endif
  320. }
  321. }