llvm.org GIT mirror llvm / e922c02
Get rid of the Pass+Context magic. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@76702 91177308-0d34-0410-b5e6-96231b3b80d8 Owen Anderson 11 years ago
120 changed file(s) with 836 addition(s) and 787 deletion(s). Raw diff Collapse all Expand all
855855 NamedValues.erase(VarName);
856856
857857 // for expr always returns 0.0.
858 return TheFunction->getContext()->getNullValue(Type::DoubleTy);
858 return TheFunction->getContext().getNullValue(Type::DoubleTy);
859859 }
860860
861861
15691569
15701570
15711571 // for expr always returns 0.0.
1572 return TheFunction->getContext()->getNullValue(Type::DoubleTy);
1572 return TheFunction->getContext().getNullValue(Type::DoubleTy);
15731573 }
15741574
15751575 Function *PrototypeAST::Codegen() {
18571857
18581858
18591859 // for expr always returns 0.0.
1860 return TheFunction->getContext()->getNullValue(Type::DoubleTy);
1860 return TheFunction->getContext().getNullValue(Type::DoubleTy);
18611861 }
18621862
18631863 Value *VarExprAST::Codegen() {
855855
856856
857857 // for expr always returns 0.0.
858 return TheFunction->getContext()->getNullValue(Type::DoubleTy);
858 return TheFunction->getContext().getNullValue(Type::DoubleTy);
859859 }
860860
861861 Value *VarExprAST::Codegen() {
2828 /// is returned. Note that this function can only fail when attempting to fold
2929 /// instructions like loads and stores, which have no constant expression form.
3030 ///
31 Constant *ConstantFoldInstruction(Instruction *I, LLVMContext *Context,
31 Constant *ConstantFoldInstruction(Instruction *I, LLVMContext &Context,
3232 const TargetData *TD = 0);
3333
3434 /// ConstantFoldConstantExpression - Attempt to fold the constant expression
3535 /// using the specified TargetData. If successful, the constant result is
3636 /// result is returned, if not, null is returned.
37 Constant *ConstantFoldConstantExpression(ConstantExpr *CE, LLVMContext *Context,
37 Constant *ConstantFoldConstantExpression(ConstantExpr *CE, LLVMContext &Context,
3838 const TargetData *TD = 0);
3939
4040 /// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
4545 ///
4646 Constant *ConstantFoldInstOperands(unsigned Opcode, const Type *DestTy,
4747 Constant*const * Ops, unsigned NumOps,
48 LLVMContext *Context,
48 LLVMContext &Context,
4949 const TargetData *TD = 0);
5050
5151 /// ConstantFoldCompareInstOperands - Attempt to constant fold a compare
5454 ///
5555 Constant *ConstantFoldCompareInstOperands(unsigned Predicate,
5656 Constant*const * Ops, unsigned NumOps,
57 LLVMContext *Context,
57 LLVMContext &Context,
5858 const TargetData *TD = 0);
5959
6060
6262 /// getelementptr constantexpr, return the constant value being addressed by the
6363 /// constant expression, or null if something is funny and we can't decide.
6464 Constant *ConstantFoldLoadThroughGEPConstantExpr(Constant *C, ConstantExpr *CE,
65 LLVMContext *Context);
65 LLVMContext &Context);
6666
6767 /// canConstantFoldCallTo - Return true if its even possible to fold a call to
6868 /// the specified function.
3636
3737 // Initialization and finalization hooks.
3838 virtual bool doInitialization(Loop *L, LPPassManager &LPM) {
39 Context = L->getHeader()->getContext();
4039 return false;
4140 }
4241
367367 static char ID; // Pass identification, replacement for typeid
368368 ScalarEvolution();
369369
370 LLVMContext *getContext() const { return Context; }
370 LLVMContext &getContext() const { return F->getContext(); }
371371
372372 /// isSCEVable - Test if values of the given type are analyzable within
373373 /// the SCEV framework. This primarily includes integer types, and it
3737 friend struct SCEVVisitor;
3838 public:
3939 explicit SCEVExpander(ScalarEvolution &se)
40 : SE(se), Builder(*se.getContext(),
41 TargetFolder(se.TD, *se.getContext())) {}
40 : SE(se), Builder(se.getContext(),
41 TargetFolder(se.TD, se.getContext())) {}
4242
4343 /// clear - Erase the contents of the InsertedExpressions map so that users
4444 /// trying to expand the same expression into multiple BasicBlocks or
6060 }
6161
6262 private:
63 LLVMContext *getContext() const { return SE.getContext(); }
63 LLVMContext &getContext() const { return SE.getContext(); }
6464
6565 /// InsertBinop - Insert the specified binary operator, doing a small amount
6666 /// of work to avoid inserting an obviously redundant operation.
6464 Value *FindInsertedValue(Value *V,
6565 const unsigned *idx_begin,
6666 const unsigned *idx_end,
67 LLVMContext *Context,
67 LLVMContext &Context,
6868 Instruction *InsertBefore = 0);
6969
7070 /// This is a convenience wrapper for finding values indexed by a single index
7171 /// only.
7272 inline Value *FindInsertedValue(Value *V, const unsigned Idx,
73 LLVMContext *Context,
73 LLVMContext &Context,
7474 Instruction *InsertBefore = 0) {
7575 const unsigned Idxs[1] = { Idx };
7676 return FindInsertedValue(V, &Idxs[0], &Idxs[1], Context, InsertBefore);
8787 public:
8888 /// getContext - Get the context in which this basic block lives,
8989 /// or null if it is not currently attached to a function.
90 LLVMContext *getContext() const;
90 LLVMContext &getContext() const;
9191
9292 /// Instruction iterators...
9393 typedef InstListType::iterator iterator;
3737 /// doInitialization - This method is called before the SCC's of the program
3838 /// has been processed, allowing the pass to do initialization as necessary.
3939 virtual bool doInitialization(CallGraph &CG) {
40 Context = &CG.getModule().getContext();
4140 return false;
4241 }
4342
141141 const TargetMachine &TM;
142142 const TargetRegisterInfo &TRI;
143143 SmallVector &Locs;
144 LLVMContext *Context;
144 LLVMContext &Context;
145145
146146 unsigned StackOffset;
147147 SmallVector UsedRegs;
148148 public:
149149 CCState(unsigned CC, bool isVarArg, const TargetMachine &TM,
150 SmallVector &locs, LLVMContext *C);
150 SmallVector &locs, LLVMContext &C);
151151
152152 void addLoc(const CCValAssign &V) {
153153 Locs.push_back(V);
154154 }
155155
156 LLVMContext *getContext() const { return Context; }
156 LLVMContext &getContext() const { return Context; }
157157 const TargetMachine &getTarget() const { return TM; }
158158 unsigned getCallingConv() const { return CallingConv; }
159159 bool isVarArg() const { return IsVarArg; }
5959 const TargetData &TD;
6060 const TargetInstrInfo &TII;
6161 const TargetLowering &TLI;
62 LLVMContext *Context;
6362
6463 public:
6564 /// startNewBlock - Set the current block to which generated machine
128128
129129 /// getContext - Return a pointer to the LLVMContext associated with this
130130 /// function, or NULL if this function is not bound to a context yet.
131 LLVMContext *getContext() const;
131 LLVMContext &getContext() const;
132132
133133 /// isVarArg - Return true if this function takes a variable number of
134134 /// arguments.
644644 Value *LHS, ///< The left-hand-side of the expression
645645 Value *RHS, ///< The right-hand-side of the expression
646646 const std::string &NameStr = "" ///< Name of the instruction
647 ) : CmpInst(InsertBefore->getParent()->getContext()->
648 makeCmpResultType(LHS->getType()),
647 ) : CmpInst(InsertBefore->getContext().makeCmpResultType(LHS->getType()),
649648 Instruction::ICmp, pred, LHS, RHS, NameStr,
650649 InsertBefore) {
651650 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
666665 Value *LHS, ///< The left-hand-side of the expression
667666 Value *RHS, ///< The right-hand-side of the expression
668667 const std::string &NameStr = "" ///< Name of the instruction
669 ) : CmpInst(InsertAtEnd.getContext()->makeCmpResultType(LHS->getType()),
668 ) : CmpInst(InsertAtEnd.getContext().makeCmpResultType(LHS->getType()),
670669 Instruction::ICmp, pred, LHS, RHS, NameStr,
671670 &InsertAtEnd) {
672671 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
820819 Value *LHS, ///< The left-hand-side of the expression
821820 Value *RHS, ///< The right-hand-side of the expression
822821 const std::string &NameStr = "" ///< Name of the instruction
823 ) : CmpInst(InsertBefore->getParent()->getContext()->
824 makeCmpResultType(LHS->getType()),
822 ) : CmpInst(InsertBefore->getContext().makeCmpResultType(LHS->getType()),
825823 Instruction::FCmp, pred, LHS, RHS, NameStr,
826824 InsertBefore) {
827825 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
840838 Value *LHS, ///< The left-hand-side of the expression
841839 Value *RHS, ///< The right-hand-side of the expression
842840 const std::string &NameStr = "" ///< Name of the instruction
843 ) : CmpInst(InsertAtEnd.getContext()->makeCmpResultType(LHS->getType()),
841 ) : CmpInst(InsertAtEnd.getContext().makeCmpResultType(LHS->getType()),
844842 Instruction::FCmp, pred, LHS, RHS, NameStr,
845843 &InsertAtEnd) {
846844 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
4747 class PMStack;
4848 class AnalysisResolver;
4949 class PMDataManager;
50 class LLVMContext;
5150
5251 // AnalysisID - Use the PassInfo to identify a pass...
5352 typedef const PassInfo* AnalysisID;
7776 void operator=(const Pass&); // DO NOT IMPLEMENT
7877 Pass(const Pass &); // DO NOT IMPLEMENT
7978
80 protected:
81 LLVMContext *Context;
82
8379 public:
8480 explicit Pass(intptr_t pid) : Resolver(0), PassID(pid) {
8581 assert(pid && "pid cannot be 0");
280276 /// doInitialization - Virtual method overridden by subclasses to do
281277 /// any necessary per-module initialization.
282278 ///
283 virtual bool doInitialization(Module &M) {
284 Context = &M.getContext();
285 return false;
286 }
287
279 virtual bool doInitialization(Module &M) { return false; }
280
288281 /// runOnFunction - Virtual method overriden by subclasses to do the
289282 /// per-function processing of the pass.
290283 ///
335328 /// doInitialization - Virtual method overridden by subclasses to do
336329 /// any necessary per-module initialization.
337330 ///
338 virtual bool doInitialization(Module &M) {
339 Context = &M.getContext();
340 return false;
341 }
331 virtual bool doInitialization(Module &M) { return false; }
342332
343333 /// doInitialization - Virtual method overridden by BasicBlockPass subclasses
344334 /// to do any necessary per-function initialization.
5050 IRBuilder(LLVMContext &C) : Context(C), Folder(C) { ClearInsertionPoint(); }
5151
5252 explicit IRBuilder(BasicBlock *TheBB, const T& F)
53 : Context(*TheBB->getParent()->getContext()), Folder(F) {
53 : Context(TheBB->getContext()), Folder(F) {
5454 SetInsertPoint(TheBB);
5555 }
5656
5757 explicit IRBuilder(BasicBlock *TheBB)
58 : Context(*TheBB->getParent()->getContext()), Folder(Context) {
58 : Context(TheBB->getContext()), Folder(Context) {
5959 SetInsertPoint(TheBB);
6060 }
6161
6262 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F)
63 : Context(*TheBB->getParent()->getContext()), Folder(F) {
63 : Context(TheBB->getContext()), Folder(F) {
6464 SetInsertPoint(TheBB, IP);
6565 }
6666
6767 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP)
68 : Context(*TheBB->getParent()->getContext()), Folder(Context) {
68 : Context(TheBB->getContext()), Folder(Context) {
6969 SetInsertPoint(TheBB, IP);
7070 }
7171
3434 /// Fold - Fold the constant using target specific information.
3535 Constant *Fold(Constant *C) const {
3636 if (ConstantExpr *CE = dyn_cast(C))
37 if (Constant *CF = ConstantFoldConstantExpression(CE, &Context, TD))
37 if (Constant *CF = ConstantFoldConstantExpression(CE, Context, TD))
3838 return CF;
3939 return C;
4040 }
3939 ///
4040 void PromoteMemToReg(const std::vector &Allocas,
4141 DominatorTree &DT, DominanceFrontier &DF,
42 LLVMContext *Context,
42 LLVMContext &Context,
4343 AliasSetTracker *AST = 0);
4444
4545 } // End llvm namespace
2222 class LLVMContext;
2323 typedef DenseMap ValueMapTy;
2424
25 Value *MapValue(const Value *V, ValueMapTy &VM, LLVMContext *Context);
25 Value *MapValue(const Value *V, ValueMapTy &VM, LLVMContext &Context);
2626 void RemapInstruction(Instruction *I, ValueMapTy &VM);
2727 } // End llvm namespace
2828
3939 class raw_ostream;
4040 class AssemblyAnnotationWriter;
4141 class ValueHandleBase;
42 class LLVMContext;
4243
4344 //===----------------------------------------------------------------------===//
4445 // Value Class
99100 /// All values are typed, get the type of this value.
100101 ///
101102 inline const Type *getType() const { return VTy; }
103
104 /// All values hold a context through their type.
105 LLVMContext &getContext() const;
102106
103107 // All values can potentially be named...
104108 inline bool hasName() const { return Name != 0; }
308308 AliasAnalysis::AliasResult
309309 BasicAliasAnalysis::alias(const Value *V1, unsigned V1Size,
310310 const Value *V2, unsigned V2Size) {
311 Context = &V1->getType()->getContext();
311 LLVMContext &Context = V1->getType()->getContext();
312312
313313 // Strip off any constant expression casts if they exist
314314 if (const ConstantExpr *CE = dyn_cast(V1))
394394 // the base pointers.
395395 while (isGEP(GEP1->getOperand(0)) &&
396396 GEP1->getOperand(1) ==
397 Context->getNullValue(GEP1->getOperand(1)->getType()))
397 Context.getNullValue(GEP1->getOperand(1)->getType()))
398398 GEP1 = cast(GEP1->getOperand(0));
399399 const Value *BasePtr1 = GEP1->getOperand(0);
400400
401401 while (isGEP(GEP2->getOperand(0)) &&
402402 GEP2->getOperand(1) ==
403 Context->getNullValue(GEP2->getOperand(1)->getType()))
403 Context.getNullValue(GEP2->getOperand(1)->getType()))
404404 GEP2 = cast(GEP2->getOperand(0));
405405 const Value *BasePtr2 = GEP2->getOperand(0);
406406
480480 for (unsigned i = 0; i != GEPOperands.size(); ++i)
481481 if (!isa(GEPOperands[i]))
482482 GEPOperands[i] =
483 Context->getNullValue(GEPOperands[i]->getType());
483 Context.getNullValue(GEPOperands[i]->getType());
484484 int64_t Offset =
485485 getTargetData().getIndexedOffset(BasePtr->getType(),
486486 &GEPOperands[0],
498498
499499 // This function is used to determine if the indices of two GEP instructions are
500500 // equal. V1 and V2 are the indices.
501 static bool IndexOperandsEqual(Value *V1, Value *V2, LLVMContext *Context) {
501 static bool IndexOperandsEqual(Value *V1, Value *V2, LLVMContext &Context) {
502502 if (V1->getType() == V2->getType())
503503 return V1 == V2;
504504 if (Constant *C1 = dyn_cast(V1))
505505 if (Constant *C2 = dyn_cast(V2)) {
506506 // Sign extend the constants to long types, if necessary
507507 if (C1->getType() != Type::Int64Ty)
508 C1 = Context->getConstantExprSExt(C1, Type::Int64Ty);
508 C1 = Context.getConstantExprSExt(C1, Type::Int64Ty);
509509 if (C2->getType() != Type::Int64Ty)
510 C2 = Context->getConstantExprSExt(C2, Type::Int64Ty);
510 C2 = Context.getConstantExprSExt(C2, Type::Int64Ty);
511511 return C1 == C2;
512512 }
513513 return false;
528528
529529 const PointerType *GEPPointerTy = cast(BasePtr1Ty);
530530
531 Context = &GEPPointerTy->getContext();
531 LLVMContext &Context = GEPPointerTy->getContext();
532532
533533 // Find the (possibly empty) initial sequence of equal values... which are not
534534 // necessarily constants.
603603 if (G1OC->getType() != G2OC->getType()) {
604604 // Sign extend both operands to long.
605605 if (G1OC->getType() != Type::Int64Ty)
606 G1OC = Context->getConstantExprSExt(G1OC, Type::Int64Ty);
606 G1OC = Context.getConstantExprSExt(G1OC, Type::Int64Ty);
607607 if (G2OC->getType() != Type::Int64Ty)
608 G2OC = Context->getConstantExprSExt(G2OC, Type::Int64Ty);
608 G2OC = Context.getConstantExprSExt(G2OC, Type::Int64Ty);
609609 GEP1Ops[FirstConstantOper] = G1OC;
610610 GEP2Ops[FirstConstantOper] = G2OC;
611611 }
692692 // TargetData::getIndexedOffset.
693693 for (i = 0; i != MaxOperands; ++i)
694694 if (!isa(GEP1Ops[i]))
695 GEP1Ops[i] = Context->getNullValue(GEP1Ops[i]->getType());
695 GEP1Ops[i] = Context.getNullValue(GEP1Ops[i]->getType());
696696 // Okay, now get the offset. This is the relative offset for the full
697697 // instruction.
698698 const TargetData &TD = getTargetData();
737737 const Type *ZeroIdxTy = GEPPointerTy;
738738 for (unsigned i = 0; i != FirstConstantOper; ++i) {
739739 if (!isa(ZeroIdxTy))
740 GEP1Ops[i] = GEP2Ops[i] = Context->getNullValue(Type::Int32Ty);
740 GEP1Ops[i] = GEP2Ops[i] = Context.getNullValue(Type::Int32Ty);
741741
742742 if (const CompositeType *CT = dyn_cast(ZeroIdxTy))
743743 ZeroIdxTy = CT->getTypeAtIndex(GEP1Ops[i]);
752752 // If they are equal, use a zero index...
753753 if (Op1 == Op2 && BasePtr1Ty == BasePtr2Ty) {
754754 if (!isa(Op1))
755 GEP1Ops[i] = GEP2Ops[i] = Context->getNullValue(Op1->getType());
755 GEP1Ops[i] = GEP2Ops[i] = Context.getNullValue(Op1->getType());
756756 // Otherwise, just keep the constants we have.
757757 } else {
758758 if (Op1) {
779779 //
780780 if (const ArrayType *AT = dyn_cast(BasePtr1Ty))
781781 GEP1Ops[i] =
782 Context->getConstantInt(Type::Int64Ty,AT->getNumElements()-1);
782 Context.getConstantInt(Type::Int64Ty,AT->getNumElements()-1);
783783 else if (const VectorType *VT = dyn_cast(BasePtr1Ty))
784784 GEP1Ops[i] =
785 Context->getConstantInt(Type::Int64Ty,VT->getNumElements()-1);
785 Context.getConstantInt(Type::Int64Ty,VT->getNumElements()-1);
786786 }
787787 }
788788
797797 return MayAlias; // Be conservative with out-of-range accesses
798798 }
799799 } else { // Conservatively assume the minimum value for this index
800 GEP2Ops[i] = Context->getNullValue(Op2->getType());
800 GEP2Ops[i] = Context.getNullValue(Op2->getType());
801801 }
802802 }
803803 }
9494 /// otherwise TD is null.
9595 static Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0,
9696 Constant *Op1, const TargetData *TD,
97 LLVMContext *Context){
97 LLVMContext &Context){
9898 // SROA
9999
100100 // Fold (and 0xffffffff00000000, (shl x, 32)) -> shl.
112112 if (IsConstantOffsetFromGlobal(Op1, GV2, Offs2, *TD) &&
113113 GV1 == GV2) {
114114 // (&GV+C1) - (&GV+C2) -> C1-C2, pointer arithmetic cannot overflow.
115 return Context->getConstantInt(Op0->getType(), Offs1-Offs2);
115 return Context.getConstantInt(Op0->getType(), Offs1-Offs2);
116116 }
117117 }
118118
123123 /// constant expression, do so.
124124 static Constant *SymbolicallyEvaluateGEP(Constant* const* Ops, unsigned NumOps,
125125 const Type *ResultTy,
126 LLVMContext *Context,
126 LLVMContext &Context,
127127 const TargetData *TD) {
128128 Constant *Ptr = Ops[0];
129129 if (!TD || !cast(Ptr->getType())->getElementType()->isSized())
150150
151151 uint64_t Offset = TD->getIndexedOffset(Ptr->getType(),
152152 (Value**)Ops+1, NumOps-1);
153 Constant *C = Context->getConstantInt(TD->getIntPtrType(), Offset+BasePtr);
154 return Context->getConstantExprIntToPtr(C, ResultTy);
153 Constant *C = Context.getConstantInt(TD->getIntPtrType(), Offset+BasePtr);
154 return Context.getConstantExprIntToPtr(C, ResultTy);
155155 }
156156
157157 /// FoldBitCast - Constant fold bitcast, symbolically evaluating it with
158158 /// targetdata. Return 0 if unfoldable.
159159 static Constant *FoldBitCast(Constant *C, const Type *DestTy,
160 const TargetData &TD, LLVMContext *Context) {
160 const TargetData &TD, LLVMContext &Context) {
161161 // If this is a bitcast from constant vector -> vector, fold it.
162162 if (ConstantVector *CV = dyn_cast(C)) {
163163 if (const VectorType *DestVTy = dyn_cast(DestTy)) {
183183 if (DstEltTy->isFloatingPoint()) {
184184 // Fold to an vector of integers with same size as our FP type.
185185 unsigned FPWidth = DstEltTy->getPrimitiveSizeInBits();
186 const Type *DestIVTy = Context->getVectorType(
187 Context->getIntegerType(FPWidth), NumDstElt);
186 const Type *DestIVTy = Context.getVectorType(
187 Context.getIntegerType(FPWidth), NumDstElt);
188188 // Recursively handle this integer conversion, if possible.
189189 C = FoldBitCast(C, DestIVTy, TD, Context);
190190 if (!C) return 0;
191191
192192 // Finally, VMCore can handle this now that #elts line up.
193 return Context->getConstantExprBitCast(C, DestTy);
193 return Context.getConstantExprBitCast(C, DestTy);
194194 }
195195
196196 // Okay, we know the destination is integer, if the input is FP, convert
197197 // it to integer first.
198198 if (SrcEltTy->isFloatingPoint()) {
199199 unsigned FPWidth = SrcEltTy->getPrimitiveSizeInBits();
200 const Type *SrcIVTy = Context->getVectorType(
201 Context->getIntegerType(FPWidth), NumSrcElt);
200 const Type *SrcIVTy = Context.getVectorType(
201 Context.getIntegerType(FPWidth), NumSrcElt);
202202 // Ask VMCore to do the conversion now that #elts line up.
203 C = Context->getConstantExprBitCast(C, SrcIVTy);
203 C = Context.getConstantExprBitCast(C, SrcIVTy);
204204 CV = dyn_cast(C);
205205 if (!CV) return 0; // If VMCore wasn't able to fold it, bail out.
206206 }
214214 SmallVector Result;
215215 if (NumDstElt < NumSrcElt) {
216216 // Handle: bitcast (<4 x i32> to <2 x i64>)
217 Constant *Zero = Context->getNullValue(DstEltTy);
217 Constant *Zero = Context.getNullValue(DstEltTy);
218218 unsigned Ratio = NumSrcElt/NumDstElt;
219219 unsigned SrcBitSize = SrcEltTy->getPrimitiveSizeInBits();
220220 unsigned SrcElt = 0;
227227 if (!Src) return 0; // Reject constantexpr elements.
228228
229229 // Zero extend the element to the right size.
230 Src = Context->getConstantExprZExt(Src, Elt->getType());
230 Src = Context.getConstantExprZExt(Src, Elt->getType());
231231
232232 // Shift it to the right place, depending on endianness.
233 Src = Context->getConstantExprShl(Src,
234 Context->getConstantInt(Src->getType(), ShiftAmt));
233 Src = Context.getConstantExprShl(Src,
234 Context.getConstantInt(Src->getType(), ShiftAmt));
235235 ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize;
236236
237237 // Mix it in.
238 Elt = Context->getConstantExprOr(Elt, Src);
238 Elt = Context.getConstantExprOr(Elt, Src);
239239 }
240240 Result.push_back(Elt);
241241 }
253253 for (unsigned j = 0; j != Ratio; ++j) {
254254 // Shift the piece of the value into the right place, depending on
255255 // endianness.
256 Constant *Elt = Context->getConstantExprLShr(Src,
257 Context->getConstantInt(Src->getType(), ShiftAmt));
256 Constant *Elt = Context.getConstantExprLShr(Src,
257 Context.getConstantInt(Src->getType(), ShiftAmt));
258258 ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize;
259259
260260 // Truncate and remember this piece.
261 Result.push_back(Context->getConstantExprTrunc(Elt, DstEltTy));
261 Result.push_back(Context.getConstantExprTrunc(Elt, DstEltTy));
262262 }
263263 }
264264 }
265265
266 return Context->getConstantVector(Result.data(), Result.size());
266 return Context.getConstantVector(Result.data(), Result.size());
267267 }
268268 }
269269
281281 /// is returned. Note that this function can only fail when attempting to fold
282282 /// instructions like loads and stores, which have no constant expression form.
283283 ///
284 Constant *llvm::ConstantFoldInstruction(Instruction *I, LLVMContext *Context,
284 Constant *llvm::ConstantFoldInstruction(Instruction *I, LLVMContext &Context,
285285 const TargetData *TD) {
286286 if (PHINode *PN = dyn_cast(I)) {
287287 if (PN->getNumIncomingValues() == 0)
288 return Context->getUndef(PN->getType());
288 return Context.getUndef(PN->getType());
289289
290290 Constant *Result = dyn_cast(PN->getIncomingValue(0));
291291 if (Result == 0) return 0;
321321 /// using the specified TargetData. If successful, the constant result is
322322 /// result is returned, if not, null is returned.
323323 Constant *llvm::ConstantFoldConstantExpression(ConstantExpr *CE,
324 LLVMContext *Context,
324 LLVMContext &Context,
325325 const TargetData *TD) {
326326 SmallVector Ops;
327327 for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i)
344344 ///
345345 Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, const Type *DestTy,
346346 Constant* const* Ops, unsigned NumOps,
347 LLVMContext *Context,
347 LLVMContext &Context,
348348 const TargetData *TD) {
349349 // Handle easy binops first.
350350 if (Instruction::isBinaryOp(Opcode)) {
353353 Context))
354354 return C;
355355
356 return Context->getConstantExpr(Opcode, Ops[0], Ops[1]);
356 return Context.getConstantExpr(Opcode, Ops[0], Ops[1]);
357357 }
358358
359359 switch (Opcode) {
375375 unsigned InWidth = Input->getType()->getScalarSizeInBits();
376376 if (TD->getPointerSizeInBits() < InWidth) {
377377 Constant *Mask =
378 Context->getConstantInt(APInt::getLowBitsSet(InWidth,
378 Context.getConstantInt(APInt::getLowBitsSet(InWidth,
379379 TD->getPointerSizeInBits()));
380 Input = Context->getConstantExprAnd(Input, Mask);
380 Input = Context.getConstantExprAnd(Input, Mask);
381381 }
382382 // Do a zext or trunc to get to the dest size.
383 return Context->getConstantExprIntegerCast(Input, DestTy, false);
383 return Context.getConstantExprIntegerCast(Input, DestTy, false);
384384 }
385385 }
386 return Context->getConstantExprCast(Opcode, Ops[0], DestTy);
386 return Context.getConstantExprCast(Opcode, Ops[0], DestTy);
387387 case Instruction::IntToPtr:
388388 // If the input is a ptrtoint, turn the pair into a ptr to ptr bitcast if
389389 // the int size is >= the ptr size. This requires knowing the width of a
395395 if (CE->getOpcode() == Instruction::PtrToInt) {
396396 Constant *Input = CE->getOperand(0);
397397 Constant *C = FoldBitCast(Input, DestTy, *TD, Context);
398 return C ? C : Context->getConstantExprBitCast(Input, DestTy);
398 return C ? C : Context.getConstantExprBitCast(Input, DestTy);
399399 }
400400 // If there's a constant offset added to the integer value before
401401 // it is casted back to a pointer, see if the expression can be
418418 if (ElemIdx.ult(APInt(ElemIdx.getBitWidth(),
419419 AT->getNumElements()))) {
420420 Constant *Index[] = {
421 Context->getNullValue(CE->getType()),
422 Context->getConstantInt(ElemIdx)
421 Context.getNullValue(CE->getType()),
422 Context.getConstantInt(ElemIdx)
423423 };
424424 return
425 Context->getConstantExprGetElementPtr(GV, &Index[0], 2);
425 Context.getConstantExprGetElementPtr(GV, &Index[0], 2);
426426 }
427427 }
428428 }
429429 }
430430 }
431431 }
432 return Context->getConstantExprCast(Opcode, Ops[0], DestTy);
432 return Context.getConstantExprCast(Opcode, Ops[0], DestTy);
433433 case Instruction::Trunc:
434434 case Instruction::ZExt:
435435 case Instruction::SExt:
439439 case Instruction::SIToFP:
440440 case Instruction::FPToUI:
441441 case Instruction::FPToSI:
442 return Context->getConstantExprCast(Opcode, Ops[0], DestTy);
442 return Context.getConstantExprCast(Opcode, Ops[0], DestTy);
443443 case Instruction::BitCast:
444444 if (TD)
445445 if (Constant *C = FoldBitCast(Ops[0], DestTy, *TD, Context))
446446 return C;
447 return Context->getConstantExprBitCast(Ops[0], DestTy);
447 return Context.getConstantExprBitCast(Ops[0], DestTy);
448448 case Instruction::Select:
449 return Context->getConstantExprSelect(Ops[0], Ops[1], Ops[2]);
449 return Context.getConstantExprSelect(Ops[0], Ops[1], Ops[2]);
450450 case Instruction::ExtractElement:
451 return Context->getConstantExprExtractElement(Ops[0], Ops[1]);
451 return Context.getConstantExprExtractElement(Ops[0], Ops[1]);
452452 case Instruction::InsertElement:
453 return Context->getConstantExprInsertElement(Ops[0], Ops[1], Ops[2]);
453 return Context.getConstantExprInsertElement(Ops[0], Ops[1], Ops[2]);
454454 case Instruction::ShuffleVector:
455 return Context->getConstantExprShuffleVector(Ops[0], Ops[1], Ops[2]);
455 return Context.getConstantExprShuffleVector(Ops[0], Ops[1], Ops[2]);
456456 case Instruction::GetElementPtr:
457457 if (Constant *C = SymbolicallyEvaluateGEP(Ops, NumOps, DestTy, Context, TD))
458458 return C;
459459
460 return Context->getConstantExprGetElementPtr(Ops[0], Ops+1, NumOps-1);
460 return Context.getConstantExprGetElementPtr(Ops[0], Ops+1, NumOps-1);
461461 }
462462 }
463463
468468 Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate,
469469 Constant*const * Ops,
470470 unsigned NumOps,
471 LLVMContext *Context,
471 LLVMContext &Context,
472472 const TargetData *TD) {
473473 // fold: icmp (inttoptr x), null -> icmp x, 0
474474 // fold: icmp (ptrtoint x), 0 -> icmp x, null
483483 if (CE0->getOpcode() == Instruction::IntToPtr) {
484484 // Convert the integer value to the right size to ensure we get the
485485 // proper extension or truncation.
486 Constant *C = Context->getConstantExprIntegerCast(CE0->getOperand(0),
486 Constant *C = Context.getConstantExprIntegerCast(CE0->getOperand(0),
487487 IntPtrTy, false);
488 Constant *NewOps[] = { C, Context->getNullValue(C->getType()) };
488 Constant *NewOps[] = { C, Context.getNullValue(C->getType()) };
489489 return ConstantFoldCompareInstOperands(Predicate, NewOps, 2,
490490 Context, TD);
491491 }
495495 if (CE0->getOpcode() == Instruction::PtrToInt &&
496496 CE0->getType() == IntPtrTy) {
497497 Constant *C = CE0->getOperand(0);
498 Constant *NewOps[] = { C, Context->getNullValue(C->getType()) };
498 Constant *NewOps[] = { C, Context.getNullValue(C->getType()) };
499499 // FIXME!
500500 return ConstantFoldCompareInstOperands(Predicate, NewOps, 2,
501501 Context, TD);
509509 if (CE0->getOpcode() == Instruction::IntToPtr) {
510510 // Convert the integer value to the right size to ensure we get the
511511 // proper extension or truncation.
512 Constant *C0 = Context->getConstantExprIntegerCast(CE0->getOperand(0),
512 Constant *C0 = Context.getConstantExprIntegerCast(CE0->getOperand(0),
513513 IntPtrTy, false);
514 Constant *C1 = Context->getConstantExprIntegerCast(CE1->getOperand(0),
514 Constant *C1 = Context.getConstantExprIntegerCast(CE1->getOperand(0),
515515 IntPtrTy, false);
516516 Constant *NewOps[] = { C0, C1 };
517517 return ConstantFoldCompareInstOperands(Predicate, NewOps, 2,
532532 }
533533 }
534534 }
535 return Context->getConstantExprCompare(Predicate, Ops[0], Ops[1]);
535 return Context.getConstantExprCompare(Predicate, Ops[0], Ops[1]);
536536 }
537537
538538
541541 /// constant expression, or null if something is funny and we can't decide.
542542 Constant *llvm::ConstantFoldLoadThroughGEPConstantExpr(Constant *C,
543543 ConstantExpr *CE,
544 LLVMContext *Context) {
545 if (CE->getOperand(1) != Context->getNullValue(CE->getOperand(1)->getType()))
544 LLVMContext &Context) {
545 if (CE->getOperand(1) != Context.getNullValue(CE->getOperand(1)->getType()))
546546 return 0; // Do not allow stepping over the value!
547547
548548 // Loop over all of the operands, tracking down which value we are
557557 if (ConstantStruct *CS = dyn_cast(C)) {
558558 C = CS->getOperand(El);
559559 } else if (isa(C)) {
560 C = Context->getNullValue(STy->getElementType(El));
560 C = Context.getNullValue(STy->getElementType(El));
561561 } else if (isa(C)) {
562 C = Context->getUndef(STy->getElementType(El));
562 C = Context.getUndef(STy->getElementType(El));
563563 } else {
564564 return 0;
565565 }
570570 if (ConstantArray *CA = dyn_cast(C))
571571 C = CA->getOperand(CI->getZExtValue());
572572 else if (isa(C))
573 C = Context->getNullValue(ATy->getElementType());
573 C = Context.getNullValue(ATy->getElementType());
574574 else if (isa(C))
575 C = Context->getUndef(ATy->getElementType());
575 C = Context.getUndef(ATy->getElementType());
576576 else
577577 return 0;
578578 } else if (const VectorType *PTy = dyn_cast(*I)) {
581581 if (ConstantVector *CP = dyn_cast(C))
582582 C = CP->getOperand(CI->getZExtValue());
583583 else if (isa(C))
584 C = Context->getNullValue(PTy->getElementType());
584 C = Context.getNullValue(PTy->getElementType());
585585 else if (isa(C))
586 C = Context->getUndef(PTy->getElementType());
586 C = Context.getUndef(PTy->getElementType());
587587 else
588588 return 0;
589589 } else {
678678 }
679679
680680 static Constant *ConstantFoldFP(double (*NativeFP)(double), double V,
681 const Type *Ty, LLVMContext *Context) {
681 const Type *Ty, LLVMContext &Context) {
682682 errno = 0;
683683 V = NativeFP(V);
684684 if (errno != 0) {
687687 }
688688
689689 if (Ty == Type::FloatTy)
690 return Context->getConstantFP(APFloat((float)V));
690 return Context.getConstantFP(APFloat((float)V));
691691 if (Ty == Type::DoubleTy)
692 return Context->getConstantFP(APFloat(V));
692 return Context.getConstantFP(APFloat(V));
693693 llvm_unreachable("Can only constant fold float/double");
694694 return 0; // dummy return to suppress warning
695695 }
697697 static Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double),
698698 double V, double W,
699699 const Type *Ty,
700 LLVMContext *Context) {
700 LLVMContext &Context) {
701701 errno = 0;
702702 V = NativeFP(V, W);
703703 if (errno != 0) {
706706 }
707707
708708 if (Ty == Type::FloatTy)
709 return Context->getConstantFP(APFloat((float)V));
709 return Context.getConstantFP(APFloat((float)V));
710710 if (Ty == Type::DoubleTy)
711 return Context->getConstantFP(APFloat(V));
711 return Context.getConstantFP(APFloat(V));
712712 llvm_unreachable("Can only constant fold float/double");
713713 return 0; // dummy return to suppress warning
714714 }
720720 llvm::ConstantFoldCall(Function *F,
721721 Constant* const* Operands, unsigned NumOperands) {
722722 if (!F->hasName()) return 0;
723 LLVMContext *Context = F->getContext();
723 LLVMContext &Context = F->getContext();
724724 const char *Str = F->getNameStart();
725725 unsigned Len = F->getNameLen();
726726
774774 if (V >= -0.0)
775775 return ConstantFoldFP(sqrt, V, Ty, Context);
776776 else // Undefined
777 return Context->getNullValue(Ty);
777 return Context.getNullValue(Ty);
778778 }
779779 break;
780780 case 's':
800800 }
801801 } else if (ConstantInt *Op = dyn_cast(Operands[0])) {
802802 if (Len > 11 && !memcmp(Str, "llvm.bswap", 10))
803 return Context->getConstantInt(Op->getValue().byteSwap());
803 return Context.getConstantInt(Op->getValue().byteSwap());
804804 else if (Len > 11 && !memcmp(Str, "llvm.ctpop", 10))
805 return Context->getConstantInt(Ty, Op->getValue().countPopulation());
805 return Context.getConstantInt(Ty, Op->getValue().countPopulation());
806806 else if (Len > 10 && !memcmp(Str, "llvm.cttz", 9))
807 return Context->getConstantInt(Ty, Op->getValue().countTrailingZeros());
807 return Context.getConstantInt(Ty, Op->getValue().countTrailingZeros());
808808 else if (Len > 10 && !memcmp(Str, "llvm.ctlz", 9))
809 return Context->getConstantInt(Ty, Op->getValue().countLeadingZeros());
809 return Context.getConstantInt(Ty, Op->getValue().countLeadingZeros());
810810 }
811811 } else if (NumOperands == 2) {
812812 if (ConstantFP *Op1 = dyn_cast(Operands[0])) {
829829 }
830830 } else if (ConstantInt *Op2C = dyn_cast(Operands[1])) {
831831 if (!strcmp(Str, "llvm.powi.f32")) {
832 return Context->getConstantFP(APFloat((float)std::pow((float)Op1V,
832 return Context.getConstantFP(APFloat((float)std::pow((float)Op1V,
833833 (int)Op2C->getZExtValue())));
834834 } else if (!strcmp(Str, "llvm.powi.f64")) {
835 return Context->getConstantFP(APFloat((double)std::pow((double)Op1V,
835 return Context.getConstantFP(APFloat((double)std::pow((double)Op1V,
836836 (int)Op2C->getZExtValue())));
837837 }
838838 }
692692 // If the object in the points-to set is the null object, then the null
693693 // pointer is a must alias.
694694 if (Pointee == &GraphNodes[NullObject])
695 RetVals.push_back(Context->getNullValue(P->getType()));
695 RetVals.push_back(P->getContext().getNullValue(P->getType()));
696696 }
697697 }
698698 AliasAnalysis::getMustAliases(P, RetVals);
4141
4242 if (const SCEVConstant *C = dyn_cast(S))
4343 return ConstantRange(C->getValue()->getValue());
44
45 LLVMContext &Context = SE.getContext();
4446
4547 ConstantRange FullSet(cast(S->getType())->getBitWidth(), true);
4648
7274 ConstantRange X = getRange(Mul->getOperand(0), T, SE);
7375 if (X.isFullSet()) return FullSet;
7476
75 const IntegerType *Ty = Context->getIntegerType(X.getBitWidth());
76 const IntegerType *ExTy = Context->getIntegerType(X.getBitWidth() *
77 const IntegerType *Ty = Context.getIntegerType(X.getBitWidth());
78 const IntegerType *ExTy = Context.getIntegerType(X.getBitWidth() *
7779 Mul->getNumOperands());
7880 ConstantRange XExt = X.zeroExtend(ExTy->getBitWidth());
7981
191191 }
192192
193193 const SCEV *ScalarEvolution::getConstant(const APInt& Val) {
194 return getConstant(Context->getConstantInt(Val));
194 return getConstant(getContext().getConstantInt(Val));
195195 }
196196
197197 const SCEV *
198198 ScalarEvolution::getConstant(const Type *Ty, uint64_t V, bool isSigned) {
199199 return getConstant(
200 Context->getConstantInt(cast(Ty), V, isSigned));
200 getContext().getConstantInt(cast(Ty), V, isSigned));
201201 }
202202
203203 const Type *SCEVConstant::getType() const { return V->getType(); }
15171517 ++Idx;
15181518 while (const SCEVConstant *RHSC = dyn_cast(Ops[Idx])) {
15191519 // We found two constants, fold them together!
1520 ConstantInt *Fold = Context->getConstantInt(LHSC->getValue()->getValue() *
1520 ConstantInt *Fold = getContext().getConstantInt(LHSC->getValue()->getValue() *
15211521 RHSC->getValue()->getValue());
15221522 Ops[0] = getConstant(Fold);
15231523 Ops.erase(Ops.begin()+1); // Erase the folded element
17391739 if (const SCEVConstant *LHSC = dyn_cast(LHS)) {
17401740 Constant *LHSCV = LHSC->getValue();
17411741 Constant *RHSCV = RHSC->getValue();
1742 return getConstant(cast(Context->getConstantExprUDiv(LHSCV,
1742 return getConstant(cast(getContext().getConstantExprUDiv(LHSCV,
17431743 RHSCV)));
17441744 }
17451745 }
18681868 assert(Idx < Ops.size());
18691869 while (const SCEVConstant *RHSC = dyn_cast(Ops[Idx])) {
18701870 // We found two constants, fold them together!
1871 ConstantInt *Fold = Context->getConstantInt(
1871 ConstantInt *Fold = getContext().getConstantInt(
18721872 APIntOps::smax(LHSC->getValue()->getValue(),
18731873 RHSC->getValue()->getValue()));
18741874 Ops[0] = getConstant(Fold);
19651965 assert(Idx < Ops.size());
19661966 while (const SCEVConstant *RHSC = dyn_cast(Ops[Idx])) {
19671967 // We found two constants, fold them together!
1968 ConstantInt *Fold = Context->getConstantInt(
1968 ConstantInt *Fold = getContext().getConstantInt(
19691969 APIntOps::umax(LHSC->getValue()->getValue(),
19701970 RHSC->getValue()->getValue()));
19711971 Ops[0] = getConstant(Fold);
21322132 /// specified signed integer value and return a SCEV for the constant.
21332133 const SCEV *ScalarEvolution::getIntegerSCEV(int Val, const Type *Ty) {
21342134 const IntegerType *ITy = cast(getEffectiveSCEVType(Ty));
2135 return getConstant(Context->getConstantInt(ITy, Val));
2135 return getConstant(getContext().getConstantInt(ITy, Val));
21362136 }
21372137
21382138 /// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V
21402140 const SCEV *ScalarEvolution::getNegativeSCEV(const SCEV *V) {
21412141 if (const SCEVConstant *VC = dyn_cast(V))
21422142 return getConstant(
2143 cast(Context->getConstantExprNeg(VC->getValue())));
2143 cast(getContext().getConstantExprNeg(VC->getValue())));
21442144
21452145 const Type *Ty = V->getType();
21462146 Ty = getEffectiveSCEVType(Ty);
21472147 return getMulExpr(V,
2148 getConstant(cast(Context->getAllOnesValue(Ty))));
2148 getConstant(cast(getContext().getAllOnesValue(Ty))));
21492149 }
21502150
21512151 /// getNotSCEV - Return a SCEV corresponding to ~V = -1-V
21522152 const SCEV *ScalarEvolution::getNotSCEV(const SCEV *V) {
21532153 if (const SCEVConstant *VC = dyn_cast(V))
21542154 return getConstant(
2155 cast(Context->getConstantExprNot(VC->getValue())));
2155 cast(getContext().getConstantExprNot(VC->getValue())));
21562156
21572157 const Type *Ty = V->getType();
21582158 Ty = getEffectiveSCEVType(Ty);
21592159 const SCEV *AllOnes =
2160 getConstant(cast(Context->getAllOnesValue(Ty)));
2160 getConstant(cast(getContext().getAllOnesValue(Ty)));
21612161 return getMinusSCEV(AllOnes, V);
21622162 }
21632163
28952895 // Turn shift left of a constant amount into a multiply.
28962896 if (ConstantInt *SA = dyn_cast(U->getOperand(1))) {
28972897 uint32_t BitWidth = cast(V->getType())->getBitWidth();
2898 Constant *X = Context->getConstantInt(
2898 Constant *X = getContext().getConstantInt(
28992899 APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth)));
29002900 return getMulExpr(getSCEV(U->getOperand(0)), getSCEV(X));
29012901 }
29052905 // Turn logical shift right of a constant into a unsigned divide.
29062906 if (ConstantInt *SA = dyn_cast(U->getOperand(1))) {
29072907 uint32_t BitWidth = cast(V->getType())->getBitWidth();
2908 Constant *X = Context->getConstantInt(
2908 Constant *X = getContext().getConstantInt(
29092909 APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth)));
29102910 return getUDivExpr(getSCEV(U->getOperand(0)), getSCEV(X));
29112911 }
34763476 /// the addressed element of the initializer or null if the index expression is
34773477 /// invalid.
34783478 static Constant *
3479 GetAddressedElementFromGlobal(LLVMContext *Context, GlobalVariable *GV,
3479 GetAddressedElementFromGlobal(LLVMContext &Context, GlobalVariable *GV,
34803480 const std::vector &Indices) {
34813481 Constant *Init = GV->getInitializer();
34823482 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
34903490 } else if (isa(Init)) {
34913491 if (const StructType *STy = dyn_cast(Init->getType())) {
34923492 assert(Idx < STy->getNumElements() && "Bad struct index!");
3493 Init = Context->getNullValue(STy->getElementType(Idx));
3493 Init = Context.getNullValue(STy->getElementType(Idx));
34943494 } else if (const ArrayType *ATy = dyn_cast(Init->getType())) {
34953495 if (Idx >= ATy->getNumElements()) return 0; // Bogus program
3496 Init = Context->getNullValue(ATy->getElementType());
3496 Init = Context.getNullValue(ATy->getElementType());
34973497 } else {
34983498 llvm_unreachable("Unknown constant aggregate type!");
34993499 }
35573557
35583558 unsigned MaxSteps = MaxBruteForceIterations;
35593559 for (unsigned IterationNum = 0; IterationNum != MaxSteps; ++IterationNum) {
3560 ConstantInt *ItCst = Context->getConstantInt(
3560 ConstantInt *ItCst = getContext().getConstantInt(
35613561 cast(IdxExpr->getType()), IterationNum);
35623562 ConstantInt *Val = EvaluateConstantChrecAtConstant(IdxExpr, ItCst, *this);
35633563
35643564 // Form the GEP offset.
35653565 Indexes[VarIdxNum] = Val;
35663566
3567 Constant *Result = GetAddressedElementFromGlobal(Context, GV, Indexes);
3567 Constant *Result = GetAddressedElementFromGlobal(getContext(), GV, Indexes);
35683568 if (Result == 0) break; // Cannot compute!
35693569
35703570 // Evaluate the condition for this iteration.
36483648 if (Constant *C = dyn_cast(V)) return C;
36493649 if (GlobalValue *GV = dyn_cast(V)) return GV;
36503650 Instruction *I = cast(V);
3651 LLVMContext *Context = I->getParent()->getContext();
3651 LLVMContext &Context = I->getParent()->getContext();
36523652
36533653 std::vector Operands;
36543654 Operands.resize(I->getNumOperands());
38683868 if (const CmpInst *CI = dyn_cast(I))
38693869 C = ConstantFoldCompareInstOperands(CI->getPredicate(),
38703870 &Operands[0], Operands.size(),
3871 Context);
3871 getContext());
38723872 else
38733873 C = ConstantFoldInstOperands(I->getOpcode(), I->getType(),
3874 &Operands[0], Operands.size(), Context);
3874 &Operands[0], Operands.size(),
3875 getContext());
38753876 Pair.first->second = C;
38763877 return getSCEV(C);
38773878 }
40674068 return std::make_pair(CNC, CNC);
40684069 }
40694070
4070 LLVMContext *Context = SE.getContext();
4071 LLVMContext &Context = SE.getContext();
40714072
40724073 ConstantInt *Solution1 =
4073 Context->getConstantInt((NegB + SqrtVal).sdiv(TwoA));
4074 Context.getConstantInt((NegB + SqrtVal).sdiv(TwoA));
40744075 ConstantInt *Solution2 =
4075 Context->getConstantInt((NegB - SqrtVal).sdiv(TwoA));
4076 Context.getConstantInt((NegB - SqrtVal).sdiv(TwoA));
40764077
40774078 return std::make_pair(SE.getConstant(Solution1),
40784079 SE.getConstant(Solution2));
41404141 #endif
41414142 // Pick the smallest positive root value.
41424143 if (ConstantInt *CB =
4143 dyn_cast(Context->getConstantExprICmp(ICmpInst::ICMP_ULT,
4144 dyn_cast(getContext().getConstantExprICmp(ICmpInst::ICMP_ULT,
41444145 R1->getValue(), R2->getValue()))) {
41454146 if (CB->getZExtValue() == false)
41464147 std::swap(R1, R2); // R1 is the minimum root now.
46804681
46814682 // Check Add for unsigned overflow.
46824683 // TODO: More sophisticated things could be done here.
4683 const Type *WideTy = Context->getIntegerType(getTypeSizeInBits(Ty) + 1);
4684 const Type *WideTy = getContext().getIntegerType(getTypeSizeInBits(Ty) + 1);
46844685 const SCEV *EDiff = getZeroExtendExpr(Diff, WideTy);
46854686 const SCEV *ERoundUp = getZeroExtendExpr(RoundUp, WideTy);
46864687 const SCEV *OperandExtendedAdd = getAddExpr(EDiff, ERoundUp);
48344835
48354836 // The exit value should be (End+A)/A.
48364837 APInt ExitVal = (End + A).udiv(A);
4837 ConstantInt *ExitValue = SE.getContext()->getConstantInt(ExitVal);
4838 ConstantInt *ExitValue = SE.getContext().getConstantInt(ExitVal);
48384839
48394840 // Evaluate at the exit value. If we really did fall out of the valid
48404841 // range, then we computed our trip count, otherwise wrap around or other
48464847 // Ensure that the previous value is in the range. This is a sanity check.
48474848 assert(Range.contains(
48484849 EvaluateConstantChrecAtConstant(this,
4849 SE.getContext()->getConstantInt(ExitVal - One), SE)->getValue()) &&
4850 SE.getContext().getConstantInt(ExitVal - One), SE)->getValue()) &&
48504851 "Linear scev computation is off in a bad way!");
48514852 return SE.getConstant(ExitValue);
48524853 } else if (isQuadratic()) {
48674868 // Pick the smallest positive root value.
48684869 if (ConstantInt *CB =
48694870 dyn_cast(
4870 SE.getContext()->getConstantExprICmp(ICmpInst::ICMP_ULT,
4871 SE.getContext().getConstantExprICmp(ICmpInst::ICMP_ULT,
48714872 R1->getValue(), R2->getValue()))) {
48724873 if (CB->getZExtValue() == false)
48734874 std::swap(R1, R2); // R1 is the minimum root now.
48814882 if (Range.contains(R1Val->getValue())) {
48824883 // The next iteration must be out of the range...
48834884 ConstantInt *NextVal =
4884 SE.getContext()->getConstantInt(R1->getValue()->getValue()+1);
4885 SE.getContext().getConstantInt(R1->getValue()->getValue()+1);
48854886
48864887 R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE);
48874888 if (!Range.contains(R1Val->getValue()))
48924893 // If R1 was not in the range, then it is a good return value. Make
48934894 // sure that R1-1 WAS in the range though, just in case.
48944895 ConstantInt *NextVal =
4895 SE.getContext()->getConstantInt(R1->getValue()->getValue()-1);
4896 SE.getContext().getConstantInt(R1->getValue()->getValue()-1);
48964897 R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE);
48974898 if (Range.contains(R1Val->getValue()))
48984899 return R1;
5454
5555 // FIXME: keep track of the cast instruction.
5656 if (Constant *C = dyn_cast(V))
57 return getContext()->getConstantExprCast(Op, C, Ty);
57 return getContext().getConstantExprCast(Op, C, Ty);
5858
5959 if (Argument *A = dyn_cast(V)) {
6060 // Check to see if there is already a cast!
125125 // Fold a binop with constant operands.
126126 if (Constant *CLHS = dyn_cast(LHS))
127127 if (Constant *CRHS = dyn_cast(RHS))
128 return getContext()->getConstantExpr(Opcode, CLHS, CRHS);
128 return getContext().getConstantExpr(Opcode, CLHS, CRHS);
129129
130130 // Do a quick scan to see if we have this binop nearby. If so, reuse it.
131131 unsigned ScanLimit = 6;
166166 // For a Constant, check for a multiple of the given factor.
167167 if (const SCEVConstant *C = dyn_cast(S)) {
168168 ConstantInt *CI =
169 SE.getContext()->getConstantInt(C->getValue()->getValue().sdiv(Factor));
169 SE.getContext().getConstantInt(C->getValue()->getValue().sdiv(Factor));
170170 // If the quotient is zero and the remainder is non-zero, reject
171171 // the value at this scale. It will be considered for subsequent
172172 // smaller scales.
284284 Ops = NewOps;
285285 AnyNonZeroIndices |= !ScaledOps.empty();
286286 Value *Scaled = ScaledOps.empty() ?
287 getContext()->getNullValue(Ty) :
287 getContext().getNullValue(Ty) :
288288 expandCodeFor(SE.getAddExpr(ScaledOps), Ty);
289289 GepIndices.push_back(Scaled);
290290
298298 if (FullOffset < SL.getSizeInBytes()) {
299299 unsigned ElIdx = SL.getElementContainingOffset(FullOffset);
300300 GepIndices.push_back(
301 getContext()->getConstantInt(Type::Int32Ty, ElIdx));
301 getContext().getConstantInt(Type::Int32Ty, ElIdx));
302302 ElTy = STy->getTypeAtIndex(ElIdx);
303303 Ops[0] =
304304 SE.getConstant(Ty, FullOffset - SL.getElementOffset(ElIdx));
327327 // Fold a GEP with constant operands.
328328 if (Constant *CLHS = dyn_cast(V))
329329 if (Constant *CRHS = dyn_cast(Idx))
330 return getContext()->getConstantExprGetElementPtr(CLHS, &CRHS, 1);
330 return getContext().getConstantExprGetElementPtr(CLHS, &CRHS, 1);
331331
332332 // Do a quick scan to see if we have this GEP nearby. If so, reuse it.
333333 unsigned ScanLimit = 6;
399399
400400 // -1 * ... ---> 0 - ...
401401 if (FirstOp == 1)
402 V = InsertBinop(Instruction::Sub, getContext()->getNullValue(Ty), V);
402 V = InsertBinop(Instruction::Sub, getContext().getNullValue(Ty), V);
403403 return V;
404404 }
405405
411411 const APInt &RHS = SC->getValue()->getValue();
412412 if (RHS.isPowerOf2())
413413 return InsertBinop(Instruction::LShr, LHS,
414 getContext()->getConstantInt(Ty, RHS.logBase2()));
414 getContext().getConstantInt(Ty, RHS.logBase2()));
415415 }
416416
417417 Value *RHS = expandCodeFor(S->getRHS(), Ty);
521521 BasicBlock *Preheader = L->getLoopPreheader();
522522 PHINode *PN = PHINode::Create(Ty, "indvar", Header->begin());
523523 InsertedValues.insert(PN);
524 PN->addIncoming(getContext()->getNullValue(Ty), Preheader);
524 PN->addIncoming(getContext().getNullValue(Ty), Preheader);
525525
526526 pred_iterator HPI = pred_begin(Header);
527527 assert(HPI != pred_end(Header) && "Loop with zero preds???");
531531
532532 // Insert a unit add instruction right before the terminator corresponding
533533 // to the back-edge.
534 Constant *One = getContext()->getConstantInt(Ty, 1);
534 Constant *One = getContext().getConstantInt(Ty, 1);
535535 Instruction *Add = BinaryOperator::CreateAdd(PN, One, "indvar.next",
536536 (*HPI)->getTerminator());
537537 InsertedValues.insert(Add);
823823 Value *BuildSubAggregate(Value *From, Value* To, const Type *IndexedType,
824824 SmallVector &Idxs,
825825 unsigned IdxSkip,
826 LLVMContext *Context,
826 LLVMContext &Context,
827827 Instruction *InsertBefore) {
828828 const llvm::StructType *STy = llvm::dyn_cast(IndexedType);
829829 if (STy) {
881881 //
882882 // All inserted insertvalue instructions are inserted before InsertBefore
883883 Value *BuildSubAggregate(Value *From, const unsigned *idx_begin,
884 const unsigned *idx_end, LLVMContext *Context,
884 const unsigned *idx_end, LLVMContext &Context,
885885 Instruction *InsertBefore) {
886886 assert(InsertBefore && "Must have someplace to insert!");
887887 const Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
888888 idx_begin,
889889 idx_end);
890 Value *To = Context->getUndef(IndexedType);
890 Value *To = Context.getUndef(IndexedType);
891891 SmallVector Idxs(idx_begin, idx_end);
892892 unsigned IdxSkip = Idxs.size();
893893
902902 /// If InsertBefore is not null, this function will duplicate (modified)
903903 /// insertvalues when a part of a nested struct is extracted.
904904 Value *llvm::FindInsertedValue(Value *V, const unsigned *idx_begin,
905 const unsigned *idx_end, LLVMContext *Context,
905 const unsigned *idx_end, LLVMContext &Context,
906906 Instruction *InsertBefore) {
907907 // Nothing to index? Just return V then (this is useful at the end of our
908908 // recursion)
916916 const CompositeType *PTy = cast(V->getType());
917917
918918 if (isa(V))
919 return Context->getUndef(ExtractValueInst::getIndexedType(PTy,
919 return Context.getUndef(ExtractValueInst::getIndexedType(PTy,
920920 idx_begin,
921921 idx_end));
922922 else if (isa(V))
923 return Context->getNullValue(ExtractValueInst::getIndexedType(PTy,
923 return Context.getNullValue(ExtractValueInst::getIndexedType(PTy,
924924 idx_begin,
925925 idx_end));
926926 else if (Constant *C = dyn_cast(V)) {
313313 if (ExceptionValueVar && DT && DF && isAllocaPromotable(ExceptionValueVar)) {
314314 // Turn the exception temporary into registers and phi nodes if possible.
315315 std::vector Allocas(1, ExceptionValueVar);
316 PromoteMemToReg(Allocas, *DT, *DF, Context);
316 PromoteMemToReg(Allocas, *DT, *DF, ExceptionValueVar->getContext());
317317 return true;
318318 }
319319 return false;
354354 // Create the temporary if we didn't already.
355355 if (!ExceptionValueVar) {
356356 ExceptionValueVar = new AllocaInst(
357 Context->getPointerTypeUnqual(Type::Int8Ty),
357 BB->getContext().getPointerTypeUnqual(Type::Int8Ty),
358358 "eh.value", F->begin()->begin());
359359 ++NumStackTempsIntroduced;
360360 }
319319
320320 void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
321321 IRBuilder<> Builder(CI->getParent(), CI);
322 LLVMContext *Context = CI->getParent()->getContext();
322 LLVMContext &Context = CI->getContext();
323323
324324 Function *Callee = CI->getCalledFunction();
325325 assert(Callee && "Cannot lower an indirect call!");
345345 }
346346 case Intrinsic::sigsetjmp:
347347 if (CI->getType() != Type::VoidTy)
348 CI->replaceAllUsesWith(Context->getNullValue(CI->getType()));
348 CI->replaceAllUsesWith(Context.getNullValue(CI->getType()));
349349 break;
350350
351351 case Intrinsic::longjmp: {
361361 break;
362362 }
363363 case Intrinsic::ctpop:
364 CI->replaceAllUsesWith(LowerCTPOP(*Context, CI->getOperand(1), CI));
364 CI->replaceAllUsesWith(LowerCTPOP(Context, CI->getOperand(1), CI));
365365 break;
366366
367367 case Intrinsic::bswap:
368 CI->replaceAllUsesWith(LowerBSWAP(*Context, CI->getOperand(1), CI));
368 CI->replaceAllUsesWith(LowerBSWAP(Context, CI->getOperand(1), CI));
369369 break;
370370
371371 case Intrinsic::ctlz:
372 CI->replaceAllUsesWith(LowerCTLZ(*Context, CI->getOperand(1), CI));
372 CI->replaceAllUsesWith(LowerCTLZ(Context, CI->getOperand(1), CI));
373373 break;
374374
375375 case Intrinsic::cttz: {
377377 Value *Src = CI->getOperand(1);
378378 Value *NotSrc = Builder.CreateNot(Src);
379379 NotSrc->setName(Src->getName() + ".not");
380 Value *SrcM1 = Context->getConstantInt(Src->getType(), 1);
380 Value *SrcM1 = Context.getConstantInt(Src->getType(), 1);
381381 SrcM1 = Builder.CreateSub(Src, SrcM1);
382 Src = LowerCTPOP(*Context, Builder.CreateAnd(NotSrc, SrcM1), CI);
382 Src = LowerCTPOP(Context, Builder.CreateAnd(NotSrc, SrcM1), CI);
383383 CI->replaceAllUsesWith(Src);
384384 break;
385385 }
392392 "save" : "restore") << " intrinsic.\n";
393393 Warned = true;
394394 if (Callee->getIntrinsicID() == Intrinsic::stacksave)
395 CI->replaceAllUsesWith(Context->getNullValue(CI->getType()));
395 CI->replaceAllUsesWith(Context.getNullValue(CI->getType()));
396396 break;
397397 }
398398
413413 case Intrinsic::readcyclecounter: {
414414 cerr << "WARNING: this target does not support the llvm.readcyclecoun"
415415 << "ter intrinsic. It is being lowered to a constant 0\n";
416 CI->replaceAllUsesWith(Context->getConstantInt(Type::Int64Ty, 0));
416 CI->replaceAllUsesWith(Context.getConstantInt(Type::Int64Ty, 0));
417417 break;
418418 }
419419
427427 case Intrinsic::eh_exception:
428428 case Intrinsic::eh_selector_i32:
429429 case Intrinsic::eh_selector_i64:
430 CI->replaceAllUsesWith(Context->getNullValue(CI->getType()));
430 CI->replaceAllUsesWith(Context.getNullValue(CI->getType()));
431431 break;
432432
433433 case Intrinsic::eh_typeid_for_i32:
434434 case Intrinsic::eh_typeid_for_i64:
435435 // Return something different to eh_selector.
436 CI->replaceAllUsesWith(Context->getConstantInt(CI->getType(), 1));
436 CI->replaceAllUsesWith(Context.getConstantInt(CI->getType(), 1));
437437 break;
438438
439439 case Intrinsic::var_annotation:
505505 case Intrinsic::flt_rounds:
506506 // Lower to "round to the nearest"
507507 if (CI->getType() != Type::VoidTy)
508 CI->replaceAllUsesWith(Context->getConstantInt(CI->getType(), 1));
508 CI->replaceAllUsesWith(Context.getConstantInt(CI->getType(), 1));
509509 break;
510510 }
511511
2020 using namespace llvm;
2121
2222 CCState::CCState(unsigned CC, bool isVarArg, const TargetMachine &tm,
23 SmallVector &locs, LLVMContext *C)
23 SmallVector &locs, LLVMContext &C)
2424 : CallingConv(CC), IsVarArg(isVarArg), TM(tm),
2525 TRI(*TM.getRegisterInfo()), Locs(locs), Context(C) {
2626 // No stack is used.
9191 } else if (isa(V)) {
9292 // Translate this as an integer zero so that it can be
9393 // local-CSE'd with actual integer zeros.
94 Reg = getRegForValue(Context->getNullValue(TD.getIntPtrType()));
94 Reg = getRegForValue(V->getContext().getNullValue(TD.getIntPtrType()));
9595 } else if (ConstantFP *CF = dyn_cast(V)) {
9696 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
9797
107107 if (isExact) {
108108 APInt IntVal(IntBitWidth, 2, x);
109109
110 unsigned IntegerReg = getRegForValue(Context->getConstantInt(IntVal));
110 unsigned IntegerReg =
111 getRegForValue(V->getContext().getConstantInt(IntVal));
111112 if (IntegerReg != 0)
112113 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg);
113114 }
479480 UpdateValueMap(I, ResultReg);
480481 } else {
481482 unsigned ResultReg =
482 getRegForValue(Context->getNullValue(I->getType()));
483 getRegForValue(I->getContext().getNullValue(I->getType()));
483484 UpdateValueMap(I, ResultReg);
484485 }
485486 return true;
752753 TM(MF.getTarget()),
753754 TD(*TM.getTargetData()),
754755 TII(*TM.getInstrInfo()),
755 TLI(*TM.getTargetLowering()),
756 Context(mf.getFunction()->getContext()) {
756 TLI(*TM.getTargetLowering()) {
757757 }
758758
759759 FastISel::~FastISel() {}
815815 MF = &mf;
816816 MMI = mmi;
817817 DW = dw;
818 Context = mf.getFunction()->getContext();
818 Context = &mf.getFunction()->getContext();
819819 }
820820
821821 SelectionDAG::~SelectionDAG() {
6161 Constant *GetFrameMap(Function &F);
6262 const Type* GetConcreteStackEntryType(Function &F);
6363 void CollectRoots(Function &F);
64 static GetElementPtrInst *CreateGEP(LLVMContext *Context,
64 static GetElementPtrInst *CreateGEP(LLVMContext &Context,
6565 IRBuilder<> &B, Value *BasePtr,
6666 int Idx1, const char *Name);
67 static GetElementPtrInst *CreateGEP(LLVMContext *Context,
67 static GetElementPtrInst *CreateGEP(LLVMContext &Context,
6868 IRBuilder<> &B, Value *BasePtr,
6969 int Idx1, int Idx2, const char *Name);
7070 };
9494
9595 public:
9696 EscapeEnumerator(Function &F, const char *N = "cleanup")
97 : F(F), CleanupBBName(N), State(0), Builder(*F.getContext()) {}
97 : F(F), CleanupBBName(N), State(0), Builder(F.getContext()) {}
9898
9999 IRBuilder<> *Next() {
100100 switch (State) {
187187
188188 Constant *ShadowStackGC::GetFrameMap(Function &F) {
189189 // doInitialization creates the abstract type of this value.
190 LLVMContext *Context = F.getContext();
190 LLVMContext &Context = F.getContext();
191191
192192 Type *VoidPtr = PointerType::getUnqual(Type::Int8Ty);
193193
202202 }
203203
204204 Constant *BaseElts[] = {
205 Context->getConstantInt(Type::Int32Ty, Roots.size(), false),
206 Context->getConstantInt(Type::Int32Ty, NumMeta, false),
205 Context.getConstantInt(Type::Int32Ty, Roots.size(), false),
206 Context.getConstantInt(Type::Int32Ty, NumMeta, false),
207207 };
208208
209209 Constant *DescriptorElts[] = {
210 Context->getConstantStruct(BaseElts, 2),
211 Context->getConstantArray(Context->getArrayType(VoidPtr, NumMeta),
210 Context.getConstantStruct(BaseElts, 2),
211 Context.getConstantArray(Context.getArrayType(VoidPtr, NumMeta),
212212 Metadata.begin(), NumMeta)
213213 };
214214
215 Constant *FrameMap = Context->getConstantStruct(DescriptorElts, 2);
215 Constant *FrameMap = Context.getConstantStruct(DescriptorElts, 2);
216216
217217 std::string TypeName("gc_map.");
218218 TypeName += utostr(NumMeta);
235235 GlobalVariable::InternalLinkage,
236236 FrameMap, "__gc_" + F.getName());
237237
238 Constant *GEPIndices[2] = { Context->getConstantInt(Type::Int32Ty, 0),
239 Context->getConstantInt(Type::Int32Ty, 0) };
240 return Context->getConstantExprGetElementPtr(GV, GEPIndices, 2);
238 Constant *GEPIndices[2] = { Context.getConstantInt(Type::Int32Ty, 0),
239 Context.getConstantInt(Type::Int32Ty, 0) };
240 return Context.getConstantExprGetElementPtr(GV, GEPIndices, 2);
241241 }
242242
243243 const Type* ShadowStackGC::GetConcreteStackEntryType(Function &F) {
339339 }
340340
341341 GetElementPtrInst *
342 ShadowStackGC::CreateGEP(LLVMContext *Context, IRBuilder<> &B, Value *BasePtr,
342 ShadowStackGC::CreateGEP(LLVMContext &Context, IRBuilder<> &B, Value *BasePtr,
343343 int Idx, int Idx2, const char *Name) {
344 Value *Indices[] = { Context->getConstantInt(Type::Int32Ty, 0),
345 Context->getConstantInt(Type::Int32Ty, Idx),
346 Context->getConstantInt(Type::Int32Ty, Idx2) };
344 Value *Indices[] = { Context.getConstantInt(Type::Int32Ty, 0),
345 Context.getConstantInt(Type::Int32Ty, Idx),
346 Context.getConstantInt(Type::Int32Ty, Idx2) };
347347 Value* Val = B.CreateGEP(BasePtr, Indices, Indices + 3, Name);
348348
349349 assert(isa(Val) && "Unexpected folded constant");
352352 }
353353
354354 GetElementPtrInst *
355 ShadowStackGC::CreateGEP(LLVMContext *Context, IRBuilder<> &B, Value *BasePtr,
355 ShadowStackGC::CreateGEP(LLVMContext &Context, IRBuilder<> &B, Value *BasePtr,
356356 int Idx, const char *Name) {
357 Value *Indices[] = { Context->getConstantInt(Type::Int32Ty, 0),
358 Context->getConstantInt(Type::Int32Ty, Idx) };
357 Value *Indices[] = { Context.getConstantInt(Type::Int32Ty, 0),
358 Context.getConstantInt(Type::Int32Ty, Idx) };
359359 Value *Val = B.CreateGEP(BasePtr, Indices, Indices + 2, Name);
360360
361361 assert(isa(Val) && "Unexpected folded constant");
365365
366366 /// runOnFunction - Insert code to maintain the shadow stack.
367367 bool ShadowStackGC::performCustomLowering(Function &F) {
368 LLVMContext *Context = F.getContext();
368 LLVMContext &Context = F.getContext();
369369
370370 // Find calls to llvm.gcroot.
371371 CollectRoots(F);
6767 BasicBlock *BB = I;
6868 DeadBlocks.push_back(BB);
6969 while (PHINode *PN = dyn_cast(BB->begin())) {
70 PN->replaceAllUsesWith(Context->getNullValue(PN->getType()));
70 PN->replaceAllUsesWith(F.getContext().getNullValue(PN->getType()));
7171 BB->getInstList().pop_front();
7272 }
7373 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
367367 GenericValue JIT::runFunction(Function *F,
368368 const std::vector &ArgValues) {
369369 assert(F && "Function *F was null at entry to run()");
370 LLVMContext *Context = F->getContext();
370 LLVMContext &Context = F->getContext();
371371
372372 void *FPtr = getPointerToFunction(F);
373373 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
469469 // arguments. Make this function and return.
470470
471471 // First, create the function.
472 FunctionType *STy=Context->getFunctionType(RetTy, false);
472 FunctionType *STy=Context.getFunctionType(RetTy, false);
473473 Function *Stub = Function::Create(STy, Function::InternalLinkage, "",
474474 F->getParent());
475475
486486 switch (ArgTy->getTypeID()) {
487487 default: llvm_unreachable("Unknown argument type for function call!");
488488 case Type::IntegerTyID:
489 C = Context->getConstantInt(AV.IntVal);
489 C = Context.getConstantInt(AV.IntVal);
490490 break;
491491 case Type::FloatTyID:
492 C = Context->getConstantFP(APFloat(AV.FloatVal));
492 C = Context.getConstantFP(APFloat(AV.FloatVal));
493493 break;
494494 case Type::DoubleTyID:
495 C = Context->getConstantFP(APFloat(AV.DoubleVal));
495 C = Context.getConstantFP(APFloat(AV.DoubleVal));
496496 break;
497497 case Type::PPC_FP128TyID:
498498 case Type::X86_FP80TyID:
499499 case Type::FP128TyID:
500 C = Context->getConstantFP(APFloat(AV.IntVal));
500 C = Context.getConstantFP(APFloat(AV.IntVal));
501501 break;
502502 case Type::PointerTyID:
503503 void *ArgPtr = GVTOP(AV);
504504 if (sizeof(void*) == 4)
505 C = Context->getConstantInt(Type::Int32Ty, (int)(intptr_t)ArgPtr);
505 C = Context.getConstantInt(Type::Int32Ty, (int)(intptr_t)ArgPtr);
506506 else
507 C = Context->getConstantInt(Type::Int64Ty, (intptr_t)ArgPtr);
507 C = Context.getConstantInt(Type::Int64Ty, (intptr_t)ArgPtr);
508508 // Cast the integer to pointer
509 C = Context->getConstantExprIntToPtr(C, ArgTy);
509 C = Context.getConstantExprIntToPtr(C, ArgTy);
510510 break;
511511 }
512512 Args.push_back(C);
10721072 for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end();
10731073 OI != OE; ++OI)
10741074 if (!isa(*OI) && !isa(*OI))
1075 *OI = RemapOperand(*OI, ValueMap, *Dest->getContext());
1075 *OI = RemapOperand(*OI, ValueMap, Dest->getContext());
10761076
10771077 // There is no need to map the arguments anymore.
10781078 for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end();
892892 MachineFunction &MF = *MBB.getParent();
893893 MachineConstantPool *ConstantPool = MF.getConstantPool();
894894 Constant *C =
895 MF.getFunction()->getContext()->getConstantInt(Type::Int32Ty, Val);
895 MF.getFunction()->getContext().getConstantInt(Type::Int32Ty, Val);
896896 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
897897
898898 BuildMI(MBB, MBBI, dl, TII.get(ARM::LDRcp))
695695 SmallVector RVLocs;
696696 bool isVarArg = TheCall->isVarArg();
697697 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(),
698 RVLocs, DAG.getContext());
698 RVLocs, *DAG.getContext());
699699 CCInfo.AnalyzeCallResult(TheCall,
700700 CCAssignFnForNode(CallingConv, /* Return*/ true));
701701
831831
832832 // Analyze operands of the call, assigning locations to each operand.
833833 SmallVector ArgLocs;
834 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, DAG.getContext());
834 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, *DAG.getContext());
835835 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC, /* Return*/ false));
836836
837837 // Get a count of how many bytes are to be pushed on the stack.
10311031 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
10321032
10331033 // CCState - Info about the registers and stack slots.
1034 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs, DAG.getContext());
1034 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs, *DAG.getContext());
10351035
10361036 // Analyze return values of ISD::RET.
10371037 CCInfo.AnalyzeReturn(Op.getNode(), CCAssignFnForNode(CC, /* Return */ true));
13831383
13841384 // Assign locations to all of the incoming arguments.
13851385 SmallVector ArgLocs;
1386 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, DAG.getContext());
1386 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, *DAG.getContext());
13871387 CCInfo.AnalyzeFormalArguments(Op.getNode(),
13881388 CCAssignFnForNode(CC, /* Return*/ false));
13891389
5858 MachineFunction &MF = *MBB.getParent();
5959 MachineConstantPool *ConstantPool = MF.getConstantPool();
6060 Constant *C =
61 MF.getFunction()->getContext()->getConstantInt(Type::Int32Ty, Val);
61 MF.getFunction()->getContext().getConstantInt(Type::Int32Ty, Val);
6262 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
6363
6464 BuildMI(MBB, MBBI, dl, TII.get(ARM::tLDRcp))
5252 MachineFunction &MF = *MBB.getParent();
5353 MachineConstantPool *ConstantPool = MF.getConstantPool();
5454 Constant *C =
55 MF.getFunction()->getContext()->getConstantInt(Type::Int32Ty, Val);
55 MF.getFunction()->getContext().getConstantInt(Type::Int32Ty, Val);
5656 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
5757
5858 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2LDRpci))
237237
238238 // Analyze operands of the call, assigning locations to each operand.
239239 SmallVector ArgLocs;
240 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, DAG.getContext());
240 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, *DAG.getContext());
241241
242242 CCInfo.AnalyzeCallOperands(TheCall, CC_Alpha);
243243
355355 // Assign locations to each value returned by this call.
356356 SmallVector RVLocs;
357357 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs,
358 DAG.getContext());
358 *DAG.getContext());
359359
360360 CCInfo.AnalyzeCallResult(TheCall, RetCC_Alpha);
361361 SmallVector ResultVals;
12371237 Out << '{';
12381238 if (AT->getNumElements()) {
12391239 Out << ' ';
1240 Constant *CZ = Context->getNullValue(AT->getElementType());
1240 Constant *CZ = CPV->getContext().getNullValue(AT->getElementType());
12411241 printConstant(CZ, Static);
12421242 for (unsigned i = 1, e = AT->getNumElements(); i != e; ++i) {
12431243 Out << ", ";
12621262 assert(isa(CPV) || isa(CPV));
12631263 const VectorType *VT = cast(CPV->getType());
12641264 Out << "{ ";
1265 Constant *CZ = Context->getNullValue(VT->getElementType());
1265 Constant *CZ = CPV->getContext().getNullValue(VT->getElementType());
12661266 printConstant(CZ, Static);
12671267 for (unsigned i = 1, e = VT->getNumElements(); i != e; ++i) {
12681268 Out << ", ";
12841284 Out << '{';
12851285 if (ST->getNumElements()) {
12861286 Out << ' ';
1287 printConstant(Context->getNullValue(ST->getElementType(0)), Static);
1287 printConstant(
1288 CPV->getContext().getNullValue(ST->getElementType(0)), Static);
12881289 for (unsigned i = 1, e = ST->getNumElements(); i != e; ++i) {
12891290 Out << ", ";
1290 printConstant(Context->getNullValue(ST->getElementType(i)), Static);
1291 printConstant(
1292 CPV->getContext().getNullValue(ST->getElementType(i)), Static);
12911293 }
12921294 }
12931295 Out << " }";
34973499 if (!ITy->isPowerOf2ByteWidth())
34983500 // We have a bit width that doesn't match an even power-of-2 byte
34993501 // size. Consequently we must & the value with the type's bit mask
3500 BitMask = Context->getConstantInt(ITy, ITy->getBitMask());
3502 BitMask = I.getContext().getConstantInt(ITy, ITy->getBitMask());
35013503 if (BitMask)
35023504 Out << "((";
35033505 writeOperand(Operand);
13841384 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
13851385 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
13861386 DebugLoc dl = Op.getDebugLoc();
1387 CCState CCInfo(CC, isVarArg, TM, RVLocs, DAG.getContext());
1387 CCState CCInfo(CC, isVarArg, TM, RVLocs, *DAG.getContext());
13881388 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_SPU);
13891389
13901390 // If this is the first return lowered for this function, add the regs to the
194194
195195 // Assign locations to all of the incoming arguments.
196196 SmallVector ArgLocs;
197 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, DAG.getContext());
197 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, *DAG.getContext());
198198 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_MSP430);
199199
200200 assert(!isVarArg && "Varargs not supported yet");
271271 DebugLoc dl = Op.getDebugLoc();
272272
273273 // CCState - Info about the registers and stack slot.
274 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs, DAG.getContext());
274 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs, *DAG.getContext());
275275
276276 // Analize return values of ISD::RET
277277 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_MSP430);
323323
324324 // Analyze operands of the call, assigning locations to each operand.
325325 SmallVector ArgLocs;
326 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, DAG.getContext());
326 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, *DAG.getContext());
327327
328328 CCInfo.AnalyzeCallOperands(TheCall, CC_MSP430);
329329
451451 // Assign locations to each value returned by this call.
452452 SmallVector RVLocs;
453453 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(),
454 RVLocs, DAG.getContext());
454 RVLocs, *DAG.getContext());
455455
456456 CCInfo.AnalyzeCallResult(TheCall, RetCC_MSP430);
457457 SmallVector ResultVals;
734734
735735 // Analyze operands of the call, assigning locations to each operand.
736736 SmallVector ArgLocs;
737 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, DAG.getContext());
737 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, *DAG.getContext());
738738
739739 // To meet O32 ABI, Mips must always allocate 16 bytes on
740740 // the stack (even if less than 4 are used as arguments)
918918 // Assign locations to each value returned by this call.
919919 SmallVector RVLocs;
920920 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(),
921 RVLocs, DAG.getContext());
921 RVLocs, *DAG.getContext());
922922
923923 CCInfo.AnalyzeCallResult(TheCall, RetCC_Mips);
924924 SmallVector ResultVals;
962962
963963 // Assign locations to all of the incoming arguments.
964964 SmallVector ArgLocs;
965 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, DAG.getContext());
965 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, *DAG.getContext());
966966
967967 if (Subtarget->isABI_O32())
968968 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_MipsO32);
11101110 DebugLoc dl = Op.getDebugLoc();
11111111
11121112 // CCState - Info about the registers and stack slot.
1113 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs, DAG.getContext());
1113 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs, *DAG.getContext());
11141114
11151115 // Analize return values of ISD::RET
11161116 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_Mips);
15261526
15271527 // Assign locations to all of the incoming arguments.
15281528 SmallVector ArgLocs;
1529 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, DAG.getContext());
1529 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, *DAG.getContext());
15301530
15311531 // Reserve space for the linkage area on the stack.
15321532 CCInfo.AllocateStack(PPCFrameInfo::getLinkageSize(false, false), PtrByteSize);
15851585 // caller's stack frame, right above the parameter list area.
15861586 SmallVector ByValArgLocs;
15871587 CCState CCByValInfo(CC, isVarArg, getTargetMachine(),
1588 ByValArgLocs, DAG.getContext());
1588 ByValArgLocs, *DAG.getContext());
15891589
15901590 // Reserve stack space for the allocations in CCInfo.
15911591 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
24542454 SmallVector ResultVals;
24552455 SmallVector RVLocs;
24562456 unsigned CallerCC = DAG.getMachineFunction().getFunction()->getCallingConv();
2457 CCState CCRetInfo(CallerCC, isVarArg, TM, RVLocs, DAG.getContext());
2457 CCState CCRetInfo(CallerCC, isVarArg, TM, RVLocs, *DAG.getContext());
24582458 CCRetInfo.AnalyzeCallResult(TheCall, RetCC_PPC);
24592459
24602460 // Copy all of the result registers out of their specified physreg.
25602560
25612561 // Assign locations to all of the outgoing arguments.
25622562 SmallVector ArgLocs;
2563 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, DAG.getContext());
2563 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, *DAG.getContext());
25642564
25652565 // Reserve space for the linkage area on the stack.
25662566 CCInfo.AllocateStack(PPCFrameInfo::getLinkageSize(false, false), PtrByteSize);
26012601 // Assign locations to all of the outgoing aggregate by value arguments.
26022602 SmallVector ByValArgLocs;
26032603 CCState CCByValInfo(CC, isVarArg, getTargetMachine(), ByValArgLocs,
2604 DAG.getContext());
2604 *DAG.getContext());
26052605
26062606 // Reserve stack space for the allocations in CCInfo.
26072607 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
30663066 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
30673067 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
30683068 DebugLoc dl = Op.getDebugLoc();
3069 CCState CCInfo(CC, isVarArg, TM, RVLocs, DAG.getContext());
3069 CCState CCInfo(CC, isVarArg, TM, RVLocs, *DAG.getContext());
30703070 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_PPC);
30713071
30723072 // If this is the first return lowered for this function, add the regs to the
3939 DebugLoc dl = Op.getDebugLoc();
4040
4141 // CCState - Info about the registers and stack slot.
42 CCState CCInfo(CC, isVarArg, DAG.getTarget(), RVLocs, DAG.getContext());
42 CCState CCInfo(CC, isVarArg, DAG.getTarget(), RVLocs, *DAG.getContext());
4343
4444 // Analize return values of ISD::RET
4545 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_Sparc32);
8989
9090 // Assign locations to all of the incoming arguments.
9191 SmallVector ArgLocs;
92 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, DAG.getContext());
92 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, *DAG.getContext());
9393 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_Sparc32);
9494
9595 static const unsigned ArgRegs[] = {
468468 // Assign locations to each value returned by this call.
469469 SmallVector RVLocs;
470470 CCState RVInfo(CallingConv, isVarArg, DAG.getTarget(),
471 RVLocs, DAG.getContext());
471 RVLocs, *DAG.getContext());
472472
473473 RVInfo.AnalyzeCallResult(TheCall, RetCC_Sparc32);
474474 SmallVector ResultVals;
212212
213213 // Assign locations to all of the incoming arguments.
214214 SmallVector ArgLocs;
215 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, DAG.getContext());
215 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, *DAG.getContext());
216216 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_SystemZ);
217217
218218 if (isVarArg)
304304
305305 // Analyze operands of the call, assigning locations to each operand.
306306 SmallVector ArgLocs;
307 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, DAG.getContext());
307 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, *DAG.getContext());
308308
309309 CCInfo.AnalyzeCallOperands(TheCall, CC_SystemZ);
310310
435435 // Assign locations to each value returned by this call.
436436 SmallVector RVLocs;
437437 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs,
438 DAG.getContext());
438 *DAG.getContext());
439439
440440 CCInfo.AnalyzeCallResult(TheCall, RetCC_SystemZ);
441441 SmallVector ResultVals;
481481 DebugLoc dl = Op.getDebugLoc();
482482
483483 // CCState - Info about the registers and stack slot.
484 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs, DAG.getContext());
484 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs, *DAG.getContext());
485485
486486 // Analize return values of ISD::RET
487487 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_SystemZ);
271271 const X86AddressMode &AM) {
272272 // Handle 'null' like i32/i64 0.
273273 if (isa(Val))
274 Val = Context->getNullValue(TD.getIntPtrType());
274 Val = Val->getContext().getNullValue(TD.getIntPtrType());
275275
276276 // If this is a store of a simple constant, fold the constant into the store.
277277 if (ConstantInt *CI = dyn_cast(Val)) {
671671
672672 // Handle 'null' like i32/i64 0.
673673 if (isa(Op1))
674 Op1 = Context->getNullValue(TD.getIntPtrType());
674 Op1 = Op0->getContext().getNullValue(TD.getIntPtrType());
675675
676676 // We have two options: compare with register or immediate. If the RHS of
677677 // the compare is an immediate that we can fold into this compare, use
10481048 SmallVector RVLocs;
10491049 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
10501050 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
1051 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs, DAG.getContext());
1051 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs, *DAG.getContext());
10521052 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
10531053
10541054 // If this is the first return lowered for this function, add the regs to the
11751175 bool isVarArg = TheCall->isVarArg();
11761176 bool Is64Bit = Subtarget->is64Bit();
11771177 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(),
1178 RVLocs, DAG.getContext());
1178 RVLocs, *DAG.getContext());
11791179 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
11801180
11811181 SmallVector ResultVals;
13841384
13851385 // Assign locations to all of the incoming arguments.
13861386 SmallVector ArgLocs;
1387 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, DAG.getContext());
1387 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, *DAG.getContext());
13881388 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
13891389
13901390 SmallVector ArgValues;
16791679
16801680 // Analyze operands of the call, assigning locations to each operand.
16811681 SmallVector ArgLocs;
1682 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, DAG.getContext());
1682 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, *DAG.getContext());
16831683 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
16841684
16851685 // Get a count of how many bytes are to be pushed on the stack.
22832283 MachineConstantPool &MCP = *MF.getConstantPool();
22842284 const VectorType *Ty = VectorType::get(Type::Int32Ty, 4);
22852285 Constant *C = LoadMI->getOpcode() == X86::V_SET0 ?
2286 MF.getFunction()->getContext()->getNullValue(Ty) :
2287 MF.getFunction()->getContext()->getAllOnesValue(Ty);
2286 MF.getFunction()->getContext().getNullValue(Ty) :
2287 MF.getFunction()->getContext().getAllOnesValue(Ty);
22882288 unsigned CPI = MCP.getConstantPoolIndex(C, 16);
22892289
22902290 // Create operands to load from the constant pool entry.
647647
648648 // Analyze operands of the call, assigning locations to each operand.
649649 SmallVector ArgLocs;
650 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, DAG.getContext());
650 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, *DAG.getContext());
651651
652652 // The ABI dictates there should be one stack slot available to the callee
653653 // on function entry (for saving lr).
774774 // Assign locations to each value returned by this call.
775775 SmallVector RVLocs;
776776 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(),
777 RVLocs, DAG.getContext());
777 RVLocs, *DAG.getContext());
778778
779779 CCInfo.AnalyzeCallResult(TheCall, RetCC_XCore);
780780 SmallVector ResultVals;
830830
831831 // Assign locations to all of the incoming arguments.
832832 SmallVector ArgLocs;
833 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, DAG.getContext());
833 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, *DAG.getContext());
834834
835835 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_XCore);
836836
947947 DebugLoc dl = Op.getDebugLoc();
948948
949949 // CCState - Info about the registers and stack slot.
950 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs, DAG.getContext());
950 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs, *DAG.getContext());
951951
952952 // Analize return values of ISD::RET
953953 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_XCore);
575575 AttributesVec.push_back(AttributeWithIndex::get(~0, attrs));
576576
577577 const Type *RetTy = FTy->getReturnType();
578 LLVMContext &Context = RetTy->getContext();
578579
579580 // Work around LLVM bug PR56: the CWriter cannot emit varargs functions which
580581 // have zero fixed arguments.
585586 }
586587
587588 // Construct the new function type using the new arguments.
588 FunctionType *NFTy = Context->getFunctionType(RetTy, Params, FTy->isVarArg());
589 FunctionType *NFTy = Context.getFunctionType(RetTy, Params, FTy->isVarArg());
589590
590591 // Create the new function body and insert it into the module...
591592 Function *NF = Function::Create(NFTy, F->getLinkage(), F->getName());
636637 // Emit a GEP and load for each element of the struct.
637638 const Type *AgTy = cast(I->getType())->getElementType();
638639 const StructType *STy = cast(AgTy);
639 Value *Idxs[2] = { Context->getConstantInt(Type::Int32Ty, 0), 0 };
640 Value *Idxs[2] = { Context.getConstantInt(Type::Int32Ty, 0), 0 };
640641 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
641 Idxs[1] = Context->getConstantInt(Type::Int32Ty, i);
642 Idxs[1] = Context.getConstantInt(Type::Int32Ty, i);
642643 Value *Idx = GetElementPtrInst::Create(*AI, Idxs, Idxs+2,
643644 (*AI)->getName()+"."+utostr(i),
644645 Call);
663664 // Use i32 to index structs, and i64 for others (pointers/arrays).
664665 // This satisfies GEP constraints.
665666 const Type *IdxTy = (isa(ElTy) ? Type::Int32Ty : Type::Int64Ty);
666 Ops.push_back(Context->getConstantInt(IdxTy, *II));
667 Ops.push_back(Context.getConstantInt(IdxTy, *II));
667668 // Keep track of the type we're currently indexing
668669 ElTy = cast(ElTy)->getTypeAtIndex(*II);
669670 }
679680 }
680681
681682 if (ExtraArgHack)
682 Args.push_back(Context->getNullValue(Type::Int32Ty));
683 Args.push_back(Context.getNullValue(Type::Int32Ty));
683684
684685 // Push any varargs arguments on the list
685686 for (; AI != CS.arg_end(); ++AI, ++ArgIndex) {
756757 const Type *AgTy = cast(I->getType())->getElementType();
757758 Value *TheAlloca = new AllocaInst(AgTy, 0, "", InsertPt);
758759 const StructType *STy = cast(AgTy);
759 Value *Idxs[2] = { Context->getConstantInt(Type::Int32Ty, 0), 0 };
760 Value *Idxs[2] = { Context.getConstantInt(Type::Int32Ty, 0), 0 };
760761
761762 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
762 Idxs[1] = Context->getConstantInt(Type::Int32Ty, i);
763 Idxs[1] = Context.getConstantInt(Type::Int32Ty, i);
763764 std::string Name = TheAlloca->getName()+"."+utostr(i);
764765 Value *Idx = GetElementPtrInst::Create(TheAlloca, Idxs, Idxs+2,
765766 Name, InsertPt);
842843
843844 // Notify the alias analysis implementation that we inserted a new argument.
844845 if (ExtraArgHack)
845 AA.copyValue(Context->getNullValue(Type::Int32Ty), NF->arg_begin());
846 AA.copyValue(Context.getNullValue(Type::Int32Ty), NF->arg_begin());
846847
847848
848849 // Tell the alias analysis that the old function is about to disappear.
195195 // Start by computing a new prototype for the function, which is the same as
196196 // the old function, but doesn't have isVarArg set.
197197 const FunctionType *FTy = Fn.getFunctionType();
198 LLVMContext &Context = FTy->getContext();
199
198200 std::vector Params(FTy->param_begin(), FTy->param_end());
199 FunctionType *NFTy = Context->getFunctionType(FTy->getReturnType(),
201 FunctionType *NFTy = Context.getFunctionType(FTy->getReturnType(),
200202 Params, false);
201203 unsigned NumArgs = Params.size();
202204
597599 const Type *RetTy = FTy->getReturnType();
598600 const Type *NRetTy = NULL;
599601 unsigned RetCount = NumRetVals(F);
602
603 LLVMContext &Context = RetTy->getContext();
604
600605 // -1 means unused, other numbers are the new index
601606 SmallVector NewRetIdxs(RetCount, -1);
602607 std::vector RetTypes;
634639 // something and {} into void.
635640 // Make the new struct packed if we used to return a packed struct
636641 // already.
637 NRetTy = Context->getStructType(RetTypes, STy->isPacked());
642 NRetTy = Context.getStructType(RetTypes, STy->isPacked());
638643 else if (RetTypes.size() == 1)
639644 // One return type? Just a simple value then, but only if we didn't use to
640645 // return a struct with that simple value before.
702707 }
703708
704709 // Create the new function type based on the recomputed parameters.
705 FunctionType *NFTy = Context->getFunctionType(NRetTy, Params,
710 FunctionType *NFTy = Context.getFunctionType(NRetTy, Params,
706711 FTy->isVarArg());
707712
708713 // No change?
752757 }
753758
754759 if (ExtraArgHack)
755 Args.push_back(Context->getUndef(Type::Int32Ty));
760 Args.push_back(Context.getUndef(Type::Int32Ty));
756761
757762 // Push any varargs arguments on the list. Don't forget their attributes.
758763 for (CallSite::arg_iterator E = CS.arg_end(); I != E; ++I, ++i) {
791796 } else if (New->getType() == Type::VoidTy) {
792797 // Our return value has uses, but they will get removed later on.
793798 // Replace by null for now.
794 Call->replaceAllUsesWith(Context->getNullValue(Call->getType()));
799 Call->replaceAllUsesWith(Context.getNullValue(Call->getType()));
795800 } else {
796801 assert(isa(RetTy) &&
797802 "Return type changed, but not into a void. The old return type"
808813 // extract/insertvalue chaining and let instcombine clean that up.
809814 //
810815 // Start out building up our return value from undef
811 Value *RetVal = Context->getUndef(RetTy);
816 Value *RetVal = Context.getUndef(RetTy);
812817 for (unsigned i = 0; i != RetCount; ++i)
813818 if (NewRetIdxs[i] != -1) {
814819 Value *V;
854859 } else {
855860 // If this argument is dead, replace any uses of it with null constants
856861 // (these are guaranteed to become unused later on).
857 I->replaceAllUsesWith(Context->getNullValue(I->getType()));
862 I->replaceAllUsesWith(Context.getNullValue(I->getType()));
858863 }
859864
860865 // If we change the return value of the function we must rewrite any return
875880 // clean that up.
876881 Value *OldRet = RI->getOperand(0);
877882 // Start out building up our return value from undef
878 RetVal = Context->getUndef(NRetTy);
883 RetVal = Context.getUndef(NRetTy);
879884 for (unsigned i = 0; i != RetCount; ++i)
880885 if (NewRetIdxs[i] != -1) {
881886 ExtractValueInst *EV = ExtractValueInst::Create(OldRet, i,
907912
908913 bool DAE::runOnModule(Module &M) {
909914 bool Changed = false;
910 Context = &M.getContext();
911915
912916 // First pass: Do a simple check to see if any functions can have their "..."
913917 // removed. We can do this if they never call va_start. This loop cannot be
7676 //
7777 bool DTE::runOnModule(Module &M) {
7878 bool Changed = false;
79 Context = &M.getContext();
8079
8180 TypeSymbolTable &ST = M.getTypeSymbolTable();
8281 std::set UsedTypes = getAnalysis().getTypes();
4343 return false; // Nothing to extract
4444 }
4545
46 Context = &M.getContext();
4746
4847 if (deleteStuff)
4948 return deleteGV();
8685 }
8786
8887 bool isolateGV(Module &M) {
88 LLVMContext &Context = M.getContext();
89
8990 // Mark all globals internal
9091 // FIXME: what should we do with private linkage?
9192 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
101102 // by putting them in the used array
102103 {
103104 std::vector AUGs;
104 const Type *SBP= Context->getPointerTypeUnqual(Type::Int8Ty);
105 const Type *SBP= Context.getPointerTypeUnqual(Type::Int8Ty);
105106 for (std::vector::iterator GI = Named.begin(),
106107 GE = Named.end(); GI != GE; ++GI) {
107108 (*GI)->setLinkage(GlobalValue::ExternalLinkage);
108 AUGs.push_back(Context->getConstantExprBitCast(*GI, SBP));
109 AUGs.push_back(Context.getConstantExprBitCast(*GI, SBP));
109110 }
110 ArrayType *AT = Context->getArrayType(SBP, AUGs.size());
111 Constant *Init = Context->getConstantArray(AT, AUGs);
111 ArrayType *AT = Context.getArrayType(SBP, AUGs.size());
112 Constant *Init = Context.getConstantArray(AT, AUGs);
112113 GlobalValue *gv = new GlobalVariable(M, AT, false,
113114 GlobalValue::AppendingLinkage,
114115 Init, "llvm.used");
5757
5858 bool GlobalDCE::runOnModule(Module &M) {
5959 bool Changed = false;
60 Context = &M.getContext();
6160
6261 // Loop over the module, adding globals which are obviously necessary.
6362 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
246246 }
247247
248248 static Constant *getAggregateConstantElement(Constant *Agg, Constant *Idx,
249 LLVMContext *Context) {
249 LLVMContext &Context) {
250250 ConstantInt *CI = dyn_cast(Idx);
251251 if (!CI) return 0;
252252 unsigned IdxV = CI->getZExtValue();
260260 } else if (isa(Agg)) {
261261 if (const StructType *STy = dyn_cast(Agg->getType())) {
262262 if (IdxV < STy->getNumElements())
263 return Context->getNullValue(STy->getElementType(IdxV));
263 return Context.getNullValue(STy->getElementType(IdxV));
264264 } else if (const SequentialType *STy =
265265 dyn_cast(Agg->getType())) {
266 return Context->getNullValue(STy->getElementType());
266 return Context.getNullValue(STy->getElementType());
267267 }
268268 } else if (isa(Agg)) {
269269 if (const StructType *STy = dyn_cast(Agg->getType())) {
270270 if (IdxV < STy->getNumElements())
271 return Context->getUndef(STy->getElementType(IdxV));
271 return Context.getUndef(STy->getElementType(IdxV));
272272 } else if (const SequentialType *STy =
273273 dyn_cast(Agg->getType())) {
274 return Context->getUndef(STy->getElementType());
274 return Context.getUndef(STy->getElementType());
275275 }
276276 }
277277 return 0;
283283 /// quick scan over the use list to clean up the easy and obvious cruft. This
284284 /// returns true if it made a change.
285285 static bool CleanupConstantGlobalUsers(Value *V, Constant *Init,
286 LLVMContext *Context) {
286 LLVMContext &Context) {
287287 bool Changed = false;
288288 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;) {
289289 User *U = *UI++;
465465 /// this transformation is safe already. We return the first global variable we
466466 /// insert so that the caller can reprocess it.
467467 static GlobalVariable *SRAGlobal(GlobalVariable *GV, const TargetData &TD,
468 LLVMContext *Context) {
468 LLVMContext &Context) {
469469 // Make sure this global only has simple uses that we can SRA.
470470 if (!GlobalUsersSafeToSRA(GV))
471471 return 0;
487487 const StructLayout &Layout = *TD.getStructLayout(STy);
488488 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
489489 Constant *In = getAggregateConstantElement(Init,
490 Context->getConstantInt(Type::Int32Ty, i),
490 Context.getConstantInt(Type::Int32Ty, i),
491491 Context);
492492 assert(In && "Couldn't get element of initializer?");
493 GlobalVariable *NGV = new GlobalVariable(*Context,
493 GlobalVariable *NGV = new GlobalVariable(Context,
494494 STy->getElementType(i), false,
495495 GlobalVariable::InternalLinkage,
496496 In, GV->getName()+"."+utostr(i),
522522 unsigned EltAlign = TD.getABITypeAlignment(STy->getElementType());
523523 for (unsigned i = 0, e = NumElements; i != e; ++i) {
524524 Constant *In = getAggregateConstantElement(Init,
525 Context->getConstantInt(Type::Int32Ty, i),
525 Context.getConstantInt(Type::Int32Ty, i),
526526 Context);
527527 assert(In && "Couldn't get element of initializer?");
528528
529 GlobalVariable *NGV = new GlobalVariable(*Context,
529 GlobalVariable *NGV = new GlobalVariable(Context,
530530 STy->getElementType(), false,
531531 GlobalVariable::InternalLinkage,
532532 In, GV->getName()+"."+utostr(i),
549549
550550 DOUT << "PERFORMING GLOBAL SRA ON: " << *GV;
551551
552 Constant *NullInt = Context->getNullValue(Type::Int32Ty);
552 Constant *NullInt = Context.getNullValue(Type::Int32Ty);
553553
554554 // Loop over all of the uses of the global, replacing the constantexpr geps,
555555 // with smaller constantexpr geps or direct references.
574574 Idxs.push_back(NullInt);
575575 for (unsigned i = 3, e = CE->getNumOperands(); i != e; ++i)
576576 Idxs.push_back(CE->getOperand(i));
577 NewPtr = Context->getConstantExprGetElementPtr(cast(NewPtr),
577 NewPtr = Context.getConstantExprGetElementPtr(cast(NewPtr),
578578 &Idxs[0], Idxs.size());
579579 } else {
580580 GetElementPtrInst *GEPI = cast(GEP);
674674 }
675675
676676 static bool OptimizeAwayTrappingUsesOfValue(Value *V, Constant *NewV,
677 LLVMContext *Context) {
677 LLVMContext &Context) {
678678 bool Changed = false;
679679 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ) {
680680 Instruction *I = cast(*UI++);
706706 }
707707 } else if (CastInst *CI = dyn_cast(I)) {
708708 Changed |= OptimizeAwayTrappingUsesOfValue(CI,
709 Context->getConstantExprCast(CI->getOpcode(),
709 Context.getConstantExprCast(CI->getOpcode(),
710710 NewV, CI->getType()), Context);
711711 if (CI->use_empty()) {
712712 Changed = true;
724724 break;
725725 if (Idxs.size() == GEPI->getNumOperands()-1)
726726 Changed |= OptimizeAwayTrappingUsesOfValue(GEPI,
727 Context->getConstantExprGetElementPtr(NewV, &Idxs[0],
727 Context.getConstantExprGetElementPtr(NewV, &Idxs[0],
728728 Idxs.size()), Context);
729729 if (GEPI->use_empty()) {
730730 Changed = true;
742742 /// if the loaded value is dynamically null, then we know that they cannot be
743743 /// reachable with a null optimize away the load.
744744 static bool OptimizeAwayTrappingUsesOfLoads(GlobalVariable *GV, Constant *LV,
745 LLVMContext *Context) {
745 LLVMContext &Context) {
746746 bool Changed = false;
747747
748748 // Keep track of whether we are able to remove all the uses of the global
796796
797797 /// ConstantPropUsersOf - Walk the use list of V, constant folding all of the
798798 /// instructions that are foldable.
799 static void ConstantPropUsersOf(Value *V, LLVMContext *Context) {
799 static void ConstantPropUsersOf(Value *V, LLVMContext &Context) {
800800 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; )
801801 if (Instruction *I = dyn_cast(*UI++))
802802 if (Constant *NewC = ConstantFoldInstruction(I, Context)) {
817817 /// malloc into a global, and any loads of GV as uses of the new global.
818818 static GlobalVariable *OptimizeGlobalAddressOfMalloc(GlobalVariable *GV,
819819 MallocInst *MI,
820 LLVMContext *Context) {
820 LLVMContext &Context) {
821821 DOUT << "PROMOTING MALLOC GLOBAL: " << *GV << " MALLOC = " << *MI;
822822 ConstantInt *NElements = cast(MI->getArraySize());
823823
824824 if (NElements->getZExtValue() != 1) {
825825 // If we have an array allocation, transform it to a single element
826826 // allocation to make the code below simpler.
827 Type *NewTy = Context->getArrayType(MI->getAllocatedType(),
827 Type *NewTy = Context.getArrayType(MI->getAllocatedType(),
828828 NElements->getZExtValue());
829829 MallocInst *NewMI =
830 new MallocInst(NewTy, Context->getNullValue(Type::Int32Ty),
830 new MallocInst(NewTy, Context.getNullValue(Type::Int32Ty),
831831 MI->getAlignment(), MI->getName(), MI);
832832 Value* Indices[2];
833 Indices[0] = Indices[1] = Context->getNullValue(Type::Int32Ty);
833 Indices[0] = Indices[1] = Context.getNullValue(Type::Int32Ty);
834834 Value *NewGEP = GetElementPtrInst::Create(NewMI, Indices, Indices + 2,
835835 NewMI->getName()+".el0", MI);
836836 MI->replaceAllUsesWith(NewGEP);
843843 // FIXME: This new global should have the alignment returned by malloc. Code
844844 // could depend on malloc returning large alignment (on the mac, 16 bytes) but
845845 // this would only guarantee some lower alignment.
846 Constant *Init = Context->getUndef(MI->getAllocatedType());
846 Constant *Init = Context.getUndef(MI->getAllocatedType());
847847 GlobalVariable *NewGV = new GlobalVariable(*GV->getParent(),
848848 MI->getAllocatedType(), false,
849849 GlobalValue::InternalLinkage, Init,
856856
857857 Constant *RepValue = NewGV;
858858 if (NewGV->getType() != GV->getType()->getElementType())
859 RepValue = Context->getConstantExprBitCast(RepValue,
859 RepValue = Context.getConstantExprBitCast(RepValue,
860860 GV->getType()->getElementType());
861861
862862 // If there is a comparison against null, we will insert a global bool to
863863 // keep track of whether the global was initialized yet or not.
864864 GlobalVariable *InitBool =
865 new GlobalVariable(*Context, Type::Int1Ty, false,
865 new GlobalVariable(Context, Type::Int1Ty, false,
866866 GlobalValue::InternalLinkage,
867 Context->getFalse(), GV->getName()+".init",
867 Context.getFalse(), GV->getName()+".init",
868868 GV->isThreadLocal());
869869 bool InitBoolUsed = false;
870870
885885 default: llvm_unreachable("Unknown ICmp Predicate!");
886886 case ICmpInst::ICMP_ULT:
887887 case ICmpInst::ICMP_SLT:
888 LV = Context->getFalse(); // X < null -> always false
888 LV = Context.getFalse(); // X < null -> always false
889889 break;
890890 case ICmpInst::ICMP_ULE:
891891 case ICmpInst::ICMP_SLE:
892892 case ICmpInst::ICMP_EQ:
893 LV = BinaryOperator::CreateNot(*Context, LV, "notinit", CI);
893 LV = BinaryOperator::CreateNot(Context, LV, "notinit", CI);
894894 break;
895895 case ICmpInst::ICMP_NE:
896896 case ICmpInst::ICMP_UGE:
907907 } else {
908908 StoreInst *SI = cast(GV->use_back());
909909 // The global is initialized when the store to it occurs.
910 new StoreInst(Context->getTrue(), InitBool, SI);
910 new StoreInst(Context.getTrue(), InitBool, SI);
911911 SI->eraseFromParent();
912912 }
913913
11321132 static Value *GetHeapSROAValue(Value *V, unsigned FieldNo,
11331133 DenseMap > &InsertedScalarizedValues,
11341134 std::vector > &PHIsToRewrite,
1135 LLVMContext *Context) {
1135 LLVMContext &Context) {
11361136 std::vector &FieldVals = InsertedScalarizedValues[V];
11371137
11381138 if (FieldNo >= FieldVals.size())
11591159 cast(cast(PN->getType())->getElementType());
11601160
11611161 Result =
1162 PHINode::Create(Context->getPointerTypeUnqual(ST->getElementType(FieldNo)),
1162 PHINode::Create(Context.getPointerTypeUnqual(ST->getElementType(FieldNo)),
11631163 PN->getName()+".f"+utostr(FieldNo), PN);
11641164 PHIsToRewrite.push_back(std::make_pair(PN, FieldNo));
11651165 } else {
11751175 static void RewriteHeapSROALoadUser(Instruction *LoadUser,
11761176 DenseMap > &InsertedScalarizedValues,
11771177 std::vector > &PHIsToRewrite,
1178 LLVMContext *Context) {
1178 LLVMContext &Context) {
11791179 // If this is a comparison against null, handle it.
11801180 if (ICmpInst *SCI = dyn_cast(LoadUser)) {
11811181 assert(isa(SCI->getOperand(1)));
11861186 Context);
11871187
11881188 Value *New = new ICmpInst(SCI, SCI->getPredicate(), NPtr,
1189 Context->getNullValue(NPtr->getType()),
1189 Context.getNullValue(NPtr->getType()),
11901190 SCI->getName());
11911191 SCI->replaceAllUsesWith(New);
11921192 SCI->eraseFromParent();
12461246 static void RewriteUsesOfLoadForHeapSRoA(LoadInst *Load,
12471247 DenseMap > &InsertedScalarizedValues,
12481248 std::vector > &PHIsToRewrite,
1249 LLVMContext *Context) {
1249 LLVMContext &Context) {
12501250 for (Value::use_iterator UI = Load->use_begin(), E = Load->use_end();
12511251 UI != E; ) {
12521252 Instruction *User = cast(*UI++);
12631263 /// PerformHeapAllocSRoA - MI is an allocation of an array of structures. Break
12641264 /// it up into multiple allocations of arrays of the fields.
12651265 static GlobalVariable *PerformHeapAllocSRoA(GlobalVariable *GV, MallocInst *MI,
1266 LLVMContext *Context){
1266 LLVMContext &Context){
12671267 DOUT << "SROA HEAP ALLOC: " << *GV << " MALLOC = " << *MI;
12681268 const StructType *STy = cast(MI->getAllocatedType());
12691269
12801280
12811281 for (unsigned FieldNo = 0, e = STy->getNumElements(); FieldNo != e;++FieldNo){
12821282 const Type *FieldTy = STy->getElementType(FieldNo);
1283 const Type *PFieldTy = Context->getPointerTypeUnqual(FieldTy);
1283 const Type *PFieldTy = Context.getPointerTypeUnqual(FieldTy);
12841284
12851285 GlobalVariable *NGV =
12861286 new GlobalVariable(*GV->getParent(),
12871287 PFieldTy, false, GlobalValue::InternalLinkage,
1288 Context->getNullValue(PFieldTy),
1288 Context.getNullValue(PFieldTy),
12891289 GV->getName() + ".f" + utostr(FieldNo), GV,
12901290 GV->isThreadLocal());
12911291 FieldGlobals.push_back(NGV);
13111311 Value *RunningOr = 0;
13121312 for (unsigned i = 0, e = FieldMallocs.size(); i != e; ++i) {
13131313 Value *Cond = new ICmpInst(MI, ICmpInst::ICMP_EQ, FieldMallocs[i],
1314 Context->getNullValue(FieldMallocs[i]->getType()),
1314 Context.getNullValue(FieldMallocs[i]->getType()),
13151315 "isnull");
13161316 if (!RunningOr)
13171317 RunningOr = Cond; // First seteq
13381338 for (unsigned i = 0, e = FieldGlobals.size(); i != e; ++i) {
13391339 Value *GVVal = new LoadInst(FieldGlobals[i], "tmp", NullPtrBlock);
13401340 Value *Cmp = new ICmpInst(*NullPtrBlock, ICmpInst::ICMP_NE, GVVal,
1341 Context->getNullValue(GVVal->getType()),
1341 Context.getNullValue(GVVal->getType()),
13421342 "tmp");
13431343 BasicBlock *FreeBlock = BasicBlock::Create("free_it", OrigBB->getParent());
13441344 BasicBlock *NextBlock = BasicBlock::Create("next", OrigBB->getParent());
13461346
13471347 // Fill in FreeBlock.
13481348 new FreeInst(GVVal, FreeBlock);
1349 new StoreInst(Context->getNullValue(GVVal->getType()), FieldGlobals[i],
1349 new StoreInst(Context.getNullValue(GVVal->getType()), FieldGlobals[i],
13501350 FreeBlock);
13511351 BranchInst::Create(NextBlock, FreeBlock);
13521352
13861386 // Insert a store of null into each global.
13871387 for (unsigned i = 0, e = FieldGlobals.size(); i != e; ++i) {
13881388 const PointerType *PT = cast(FieldGlobals[i]->getType());
1389 Constant *Null = Context->getNullValue(PT->getElementType());
1389 Constant *Null = Context.getNullValue(PT->getElementType());
13901390 new StoreInst(Null, FieldGlobals[i], SI);
13911391 }
13921392 // Erase the original store.
14441444 MallocInst *MI,
14451445 Module::global_iterator &GVI,
14461446 TargetData &TD,
1447 LLVMContext *Context) {
1447 LLVMContext &Context) {
14481448 // If this is a malloc of an abstract type, don't touch it.
14491449 if (!MI->getAllocatedType()->isSized())
14501450 return false;
15071507 if (const ArrayType *AT = dyn_cast(MI->getAllocatedType())) {
15081508 MallocInst *NewMI =
15091509 new MallocInst(AllocSTy,
1510 Context->getConstantInt(Type::Int32Ty, AT->getNumElements()),
1510 Context.getConstantInt(Type::Int32Ty, AT->getNumElements()),
15111511 "", MI);
15121512 NewMI->takeName(MI);
15131513 Value *Cast = new BitCastInst(NewMI, MI->getType(), "tmp", MI);
15281528 // that only one value (besides its initializer) is ever stored to the global.
15291529 static bool OptimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal,
15301530 Module::global_iterator &GVI,
1531 TargetData &TD, LLVMContext *Context) {
1531 TargetData &TD, LLVMContext &Context) {
15321532 // Ignore no-op GEPs and bitcasts.
15331533 StoredOnceVal = StoredOnceVal->stripPointerCasts();
15341534
15411541 if (Constant *SOVC = dyn_cast(StoredOnceVal)) {
15421542 if (GV->getInitializer()->getType() != SOVC->getType())
15431543 SOVC =
1544 Context->getConstantExprBitCast(SOVC, GV->getInitializer()->getType());
1544 Context.getConstantExprBitCast(SOVC, GV->getInitializer()->getType());
15451545
15461546 // Optimize away any trapping uses of the loaded value.
15471547 if (OptimizeAwayTrappingUsesOfLoads(GV, SOVC, Context))
15601560 /// can shrink the global into a boolean and select between the two values
15611561 /// whenever it is used. This exposes the values to other scalar optimizations.
15621562 static bool TryToShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal,
1563 LLVMContext *Context) {
1563 LLVMContext &Context) {
15641564 const Type *GVElType = GV->getType()->getElementType();
15651565
15661566 // If GVElType is already i1, it is already shrunk. If the type of the GV is
15811581 DOUT << " *** SHRINKING TO BOOL: " << *GV;
15821582
15831583 // Create the new global, initializing it to false.
1584 GlobalVariable *NewGV = new GlobalVariable(*Context, Type::Int1Ty, false,
1585 GlobalValue::InternalLinkage, Context->getFalse(),
1584 GlobalVariable *NewGV = new GlobalVariable(Context, Type::Int1Ty, false,
1585 GlobalValue::InternalLinkage, Context.getFalse(),
15861586 GV->getName()+".b",
15871587 GV->isThreadLocal());
15881588 GV->getParent()->getGlobalList().insert(GV, NewGV);
16041604 // Only do this if we weren't storing a loaded value.
16051605 Value *StoreVal;
16061606 if (StoringOther || SI->getOperand(0) == InitVal)
1607 StoreVal = Context->getConstantInt(Type::Int1Ty, StoringOther);
1607 StoreVal = Context.getConstantInt(Type::Int1Ty, StoringOther);
16081608 else {
16091609 // Otherwise, we are storing a previously loaded copy. To do this,
16101610 // change the copy from copying the original value to just copying the
17201720 // Delete any stores we can find to the global. We may not be able to
17211721 // make it completely dead though.
17221722 bool Changed = CleanupConstantGlobalUsers(GV, GV->getInitializer(),
1723 Context);
1723 GV->getContext());
17241724
17251725 // If the global is dead now, delete it.
17261726 if (GV->use_empty()) {
17351735 GV->setConstant(true);
17361736
17371737 // Clean up any obviously simplifiable users now.
1738 CleanupConstantGlobalUsers(GV, GV->getInitializer(), Context);
1738 CleanupConstantGlobalUsers(GV, GV->getInitializer(), GV->getContext());
17391739
17401740 // If the global is dead now, just nuke it.
17411741 if (GV->use_empty()) {
17501750 } else if (!GV->getInitializer()->getType()->isSingleValueType()) {
17511751 if (GlobalVariable *FirstNewGV = SRAGlobal(GV,
17521752 getAnalysis(),
1753 Context)) {
1753 GV->getContext())) {
17541754 GVI = FirstNewGV; // Don't skip the newly produced globals!
17551755 return true;
17561756 }
17651765 GV->setInitializer(SOVConstant);
17661766
17671767 // Clean up any obviously simplifiable users now.
1768 CleanupConstantGlobalUsers(GV, GV->getInitializer(), Context);
1768 CleanupConstantGlobalUsers(GV, GV->getInitializer(),
1769 GV->getContext());
17691770
17701771 if (GV->use_empty()) {
17711772 DOUT << " *** Substituting initializer allowed us to "
17821783 // Try to optimize globals based on the knowledge that only one value
17831784 // (besides its initializer) is ever stored to the global.
17841785 if (OptimizeOnceStoredGlobal(GV, GS.StoredOnceValue, GVI,
1785 getAnalysis(), Context))
1786 getAnalysis(), GV->getContext()))
17861787 return true;
17871788
17881789 // Otherwise, if the global was not a boolean, we can shrink it to be a
17891790 // boolean.
17901791 if (Constant *SOVConstant = dyn_cast(GS.StoredOnceValue))
1791 if (TryToShrinkGlobalToBoolean(GV, SOVConstant, Context)) {
1792 if (TryToShrinkGlobalToBoolean(GV, SOVConstant, GV->getContext())) {
17921793 ++NumShrunkToBool;
17931794 return true;
17941795 }
19421943 /// specified array, returning the new global to use.
19431944 static GlobalVariable *InstallGlobalCtors(GlobalVariable *GCL,
19441945 const std::vector &Ctors,
1945 LLVMContext *Context) {
1946 LLVMContext &Context) {
19461947 // If we made a change, reassemble the initializer list.
19471948 std::vector CSVals;
1948 CSVals.push_back(Context->getConstantInt(Type::Int32Ty, 65535));
1949 CSVals.push_back(Context.getConstantInt(Type::Int32Ty, 65535));
19491950 CSVals.push_back(0);
19501951
19511952 // Create the new init list.
19541955 if (Ctors[i]) {
19551956 CSVals[1] = Ctors[i];
19561957 } else {
1957 const Type *FTy = Context->getFunctionType(Type::VoidTy, false);
1958 const PointerType *PFTy = Context->getPointerTypeUnqual(FTy);
1959 CSVals[1] = Context->getNullValue(PFTy);
1960 CSVals[0] = Context->getConstantInt(Type::Int32Ty, 2147483647);
1961 }
1962 CAList.push_back(Context->getConstantStruct(CSVals));
1958 const Type *FTy = Context.getFunctionType(Type::VoidTy, false);
1959 const PointerType *PFTy = Context.getPointerTypeUnqual(FTy);
1960 CSVals[1] = Context.getNullValue(PFTy);
1961 CSVals[0] = Context.getConstantInt(Type::Int32Ty, 2147483647);
1962 }
1963 CAList.push_back(Context.getConstantStruct(CSVals));
19631964 }
19641965
19651966 // Create the array initializer.
19661967 const Type *StructTy =
19671968 cast(GCL->getType()->getElementType())->getElementType();
1968 Constant *CA = Context->getConstantArray(ArrayType::get(StructTy,
1969 Constant *CA = Context.getConstantArray(ArrayType::get(StructTy,
19691970 CAList.size()), CAList);
19701971
19711972 // If we didn't change the number of elements, don't create a new GV.
19751976 }
19761977
19771978 // Create the new global and insert it next to the existing list.
1978 GlobalVariable *NGV = new GlobalVariable(*Context, CA->getType(),
1979 GlobalVariable *NGV = new GlobalVariable(Context, CA->getType(),
19791980 GCL->isConstant(),
19801981 GCL->getLinkage(), CA, "",
19811982 GCL->isThreadLocal());
19861987 if (!GCL->use_empty()) {
19871988 Constant *V = NGV;
19881989 if (V->getType() != GCL->getType())
1989 V = Context->getConstantExprBitCast(V, GCL->getType());
1990 V = Context.getConstantExprBitCast(V, GCL->getType());
19901991 GCL->replaceAllUsesWith(V);
19911992 }
19921993 GCL->eraseFromParent();
20102011 /// enough for us to understand. In particular, if it is a cast of something,
20112012 /// we punt. We basically just support direct accesses to globals and GEP's of
20122013 /// globals. This should be kept up to date with CommitValueTo.
2013 static bool isSimpleEnoughPointerToCommit(Constant *C, LLVMContext *Context) {
2014 static bool isSimpleEnoughPointerToCommit(Constant *C, LLVMContext &Context) {
20142015 if (GlobalVariable *GV = dyn_cast(C)) {
20152016 if (!GV->hasExternalLinkage() && !GV->hasLocalLinkage())
20162017 return false; // do not allow weak/linkonce/dllimport/dllexport linkage.
20352036 /// At this point, the GEP operands of Addr [0, OpNo) have been stepped into.
20362037 static Constant *EvaluateStoreInto(Constant *Init, Constant *Val,
20372038 ConstantExpr *Addr, unsigned OpNo,
2038 LLVMContext *Context) {
2039 LLVMContext &Context) {
20392040 // Base case of the recursion.
20402041 if (OpNo == Addr->getNumOperands()) {
20412042 assert(Val->getType() == Init->getType() && "Type mismatch!");
20512052 Elts.push_back(cast(*i));
20522053 } else if (isa(Init)) {
20532054 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
2054 Elts.push_back(Context->getNullValue(STy->getElementType(i)));
2055 Elts.push_back(Context.getNullValue(STy->getElementType(i)));
20552056 } else if (isa(Init)) {
20562057 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
2057 Elts.push_back(Context->getUndef(STy->getElementType(i)));
2058 Elts.push_back(Context.getUndef(STy->getElementType(i)));
20582059 } else {
20592060 llvm_unreachable("This code is out of sync with "
20602061 " ConstantFoldLoadThroughGEPConstantExpr");
20672068 Elts[Idx] = EvaluateStoreInto(Elts[Idx], Val, Addr, OpNo+1, Context);
20682069
20692070 // Return the modified struct.
2070 return Context->getConstantStruct(&Elts[0], Elts.size(), STy->isPacked());
2071 return Context.getConstantStruct(&Elts[0], Elts.size(), STy->isPacked());
20712072 } else {
20722073 ConstantInt *CI = cast(Addr->getOperand(OpNo));
20732074 const ArrayType *ATy = cast(Init->getType());
20782079 for (User::op_iterator i = CA->op_begin(), e = CA->op_end(); i != e; ++i)
20792080 Elts.push_back(cast(*i));
20802081 } else if (isa(Init)) {
2081 Constant *Elt = Context->getNullValue(ATy->getElementType());
2082 Constant *Elt = Context.getNullValue(ATy->getElementType());
20822083 Elts.assign(ATy->getNumElements(), Elt);
20832084 } else if (isa(Init)) {
2084 Constant *Elt = Context->getUndef(ATy->getElementType());
2085 Constant *Elt = Context.getUndef(ATy->getElementType());
20852086 Elts.assign(ATy->getNumElements(), Elt);
20862087 } else {
20872088 llvm_unreachable("This code is out of sync with "
20912092 assert(CI->getZExtValue() < ATy->getNumElements());
20922093 Elts[CI->getZExtValue()] =
20932094 EvaluateStoreInto(Elts[CI->getZExtValue()], Val, Addr, OpNo+1, Context);
2094 return Context->getConstantArray(ATy, Elts);
2095 return Context.getConstantArray(ATy, Elts);
20952096 }
20962097 }
20972098
20982099 /// CommitValueTo - We have decided that Addr (which satisfies the predicate
20992100 /// isSimpleEnoughPointerToCommit) should get Val as its value. Make it happen.
21002101 static void CommitValueTo(Constant *Val, Constant *Addr,
2101 LLVMContext *Context) {
2102 LLVMContext &Context) {
21022103 if (GlobalVariable *GV = dyn_cast(Addr)) {
21032104 assert(GV->hasInitializer());
21042105 GV->setInitializer(Val);
21182119 /// decide, return null.
21192120 static Constant *ComputeLoadResult(Constant *P,
21202121 const DenseMap &Memory,
2121 LLVMContext *Context) {
2122 LLVMContext &Context) {
21222123 // If this memory location has been recently stored, use the stored value: it
21232124 // is the most up-to-date.
21242125 DenseMap::const_iterator I = Memory.find(P);
21572158 if (std::find(CallStack.begin(), CallStack.end(), F) != CallStack.end())
21582159 return false;
21592160
2160 LLVMContext *Context = F->getContext();
2161 LLVMContext &Context = F->getContext();
21612162
21622163 CallStack.push_back(F);
21632164
21912192 Constant *Val = getVal(Values, SI->getOperand(0));
21922193 MutatedMemory[Ptr] = Val;
21932194 } else if (BinaryOperator *BO = dyn_cast(CurInst)) {
2194 InstResult = Context->getConstantExpr(BO->getOpcode(),
2195 InstResult = Context.getConstantExpr(BO->getOpcode(),
21952196 getVal(Values, BO->getOperand(0)),
21962197 getVal(Values, BO->getOperand(1)));
21972198 } else if (CmpInst *CI = dyn_cast(CurInst)) {
2198 InstResult = Context->getConstantExprCompare(CI->getPredicate(),
2199 InstResult = Context.getConstantExprCompare(CI->getPredicate(),
21992200 getVal(Values, CI->getOperand(0)),
22002201 getVal(Values, CI->getOperand(1)));
22012202 } else if (CastInst *CI = dyn_cast(CurInst)) {
2202 InstResult = Context->getConstantExprCast(CI->getOpcode(),
2203 InstResult = Context.getConstantExprCast(CI->getOpcode(),
22032204 getVal(Values, CI->getOperand(0)),
22042205 CI->getType());
22052206 } else if (SelectInst *SI = dyn_cast(CurInst)) {
22062207 InstResult =
2207 Context->getConstantExprSelect(getVal(Values, SI->getOperand(0)),
2208 Context.getConstantExprSelect(getVal(Values, SI->getOperand(0)),
22082209 getVal(Values, SI->getOperand(1)),
22092210 getVal(Values, SI->getOperand(2)));
22102211 } else if (GetElementPtrInst *GEP = dyn_cast(CurInst)) {
22142215 i != e; ++i)
22152216 GEPOps.push_back(getVal(Values, *i));
22162217 InstResult =
2217 Context->getConstantExprGetElementPtr(P, &GEPOps[0], GEPOps.size());
2218 Context.getConstantExprGetElementPtr(P, &GEPOps[0], GEPOps.size());
22182219 } else if (LoadInst *LI = dyn_cast(CurInst)) {
22192220 if (LI->isVolatile()) return false; // no volatile accesses.
22202221 InstResult = ComputeLoadResult(getVal(Values, LI->getOperand(0)),
22232224 } else if (AllocaInst *AI = dyn_cast(CurInst)) {
22242225 if (AI->isArrayAllocation()) return false; // Cannot handle array allocs.
22252226 const Type *Ty = AI->getType()->getElementType();
2226 AllocaTmps.push_back(new GlobalVariable(*Context, Ty, false,
2227 AllocaTmps.push_back(new GlobalVariable(Context, Ty, false,
22272228 GlobalValue::InternalLinkage,
2228 Context->getUndef(Ty),
2229 Context.getUndef(Ty),
22292230 AI->getName()));
22302231 InstResult = AllocaTmps.back();
22312232 } else if (CallInst *CI = dyn_cast(CurInst)) {
23672368 // silly, e.g. storing the address of the alloca somewhere and using it
23682369 // later. Since this is undefined, we'll just make it be null.
23692370 if (!Tmp->use_empty())
2370 Tmp->replaceAllUsesWith(F->getContext()->getNullValue(Tmp->getType()));
2371 Tmp->replaceAllUsesWith(F->getContext().getNullValue(Tmp->getType()));
23712372 delete Tmp;
23722373 }
23732374
24112412
24122413 if (!MadeChange) return false;
24132414
2414 GCL = InstallGlobalCtors(GCL, Ctors, Context);
2415 GCL = InstallGlobalCtors(GCL, Ctors, GCL->getContext());
24152416 return true;
24162417 }
24172418
24752476
24762477 bool GlobalOpt::runOnModule(Module &M) {
24772478 bool Changed = false;
2478 Context = &M.getContext();
24792479
24802480 // Try to find the llvm.globalctors list.
24812481 GlobalVariable *GlobalCtors = FindGlobalCtors(M);
5454 bool IPCP::runOnModule(Module &M) {
5555 bool Changed = false;
5656 bool LocalChange = true;
57
58 Context = &M.getContext();
5957
6058 // FIXME: instead of using smart algorithms, we just iterate until we stop
6159 // making changes.
135133 continue;
136134
137135 Value *V = ArgumentConstants[i].first;
138 if (V == 0) V = Context->getUndef(AI->getType());
136 if (V == 0) V = F.getContext().getUndef(AI->getType());
139137 AI->replaceAllUsesWith(V);
140138 ++NumArgumentsProped;
141139 MadeChange = true;
160158 // propagate information about its results into callers.
161159 if (F.mayBeOverridden())
162160 return false;
161
162 LLVMContext &Context = F.getContext();
163163
164164 // Check to see if this function returns a constant.
165165 SmallVector RetVals;
166166 const StructType *STy = dyn_cast(F.getReturnType());
167167 if (STy)
168168 for (unsigned i = 0, e = STy->getNumElements(); i < e; ++i)
169 RetVals.push_back(Context->getUndef(STy->getElementType(i)));
169 RetVals.push_back(Context.getUndef(STy->getElementType(i)));
170170 else
171 RetVals.push_back(Context->getUndef(F.getReturnType()));
171 RetVals.push_back(Context.getUndef(F.getReturnType()));
172172
173173 unsigned NumNonConstant = 0;
174174 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
4343 X("indmemrem","Indirect Malloc and Free Removal");
4444
4545 bool IndMemRemPass::runOnModule(Module &M) {
46 Context = &M.getContext();
47
4846 // In theory, all direct calls of malloc and free should be promoted
4947 // to intrinsics. Therefore, this goes through and finds where the
5048 // address of free or malloc are taken and replaces those with bounce
101101 CallGraph *CG = getAnalysisIfAvailable();
102102 CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : 0;
103103
104 Context = &M.getContext();
105
106104 if (ExternalNames.empty()) {
107105 // Return if we're not in 'all but main' mode and have no external api
108106 if (!AllButMain)
133133 bool LowerSetJmp::runOnModule(Module& M) {
134134 bool Changed = false;
135135
136 Context = &M.getContext();
137
138136 // These are what the functions are called.
139137 Function* SetJmp = M.getFunction("llvm.setjmp");
140138 Function* LongJmp = M.getFunction("llvm.longjmp");
202200 // This function is always successful, unless it isn't.
203201 bool LowerSetJmp::doInitialization(Module& M)
204202 {
205 const Type *SBPTy = Context->getPointerTypeUnqual(Type::Int8Ty);
206 const Type *SBPPTy = Context->getPointerTypeUnqual(SBPTy);
203 LLVMContext &Context = M.getContext();
204 const Type *SBPTy = Context.getPointerTypeUnqual(Type::Int8Ty);
205 const Type *SBPPTy = Context.getPointerTypeUnqual(SBPTy);
207206
208207 // N.B. See llvm/runtime/GCCLibraries/libexception/SJLJ-Exception.h for
209208 // a description of the following library functions.
259258 // throwing the exception for us.
260259 void LowerSetJmp::TransformLongJmpCall(CallInst* Inst)
261260 {
262 const Type* SBPTy = Context->getPointerTypeUnqual(Type::Int8Ty);
261 const Type* SBPTy = Inst->getContext().getPointerTypeUnqual(Type::Int8Ty);
263262
264263 // Create the call to "__llvm_sjljeh_throw_longjmp". This takes the
265264 // same parameters as "longjmp", except that the buffer is cast to a
290289 Removed = &BB->back();
291290 // If the removed instructions have any users, replace them now.
292291 if (!Removed->use_empty())
293 Removed->replaceAllUsesWith(Context->getUndef(Removed->getType()));
292 Removed->replaceAllUsesWith(
293 Inst->getContext().getUndef(Removed->getType()));
294294 Removed->eraseFromParent();
295295 } while (Removed != Inst);
296296
311311 assert(Inst && "Couldn't find even ONE instruction in entry block!");
312312
313313 // Fill in the alloca and call to initialize the SJ map.
314 const Type *SBPTy = Context->getPointerTypeUnqual(Type::Int8Ty);
314 const Type *SBPTy = Func->getContext().getPointerTypeUnqual(Type::Int8Ty);
315315 AllocaInst* Map = new AllocaInst(SBPTy, 0, "SJMap", Inst);
316316 CallInst::Create(InitSJMap, Map, "", Inst);
317317 return SJMap[Func] = Map;
377377 Function* Func = ABlock->getParent();
378378
379379 // Add this setjmp to the setjmp map.
380 const Type* SBPTy = Context->getPointerTypeUnqual(Type::Int8Ty);
380 const Type* SBPTy = Inst->getContext().getPointerTypeUnqual(Type::Int8Ty);
381381 CastInst* BufPtr =
382382 new BitCastInst(Inst->getOperand(1), SBPTy, "SBJmpBuf", Inst);
383383 std::vector Args =
384384 make_vector(GetSetJmpMap(Func), BufPtr,
385 Context->getConstantInt(Type::Int32Ty,
385 Inst->getContext().getConstantInt(Type::Int32Ty,
386386 SetJmpIDMap[Func]++), 0);
387387 CallInst::Create(AddSJToMap, Args.begin(), Args.end(), "", Inst);
388388
429429 PHINode* PHI = PHINode::Create(Type::Int32Ty, "SetJmpReturn", Inst);
430430
431431 // Coming from a call to setjmp, the return is 0.
432 PHI->addIncoming(Context->getNullValue(Type::Int32Ty), ABlock);
432