blob: a32cb3c871c8a9f1667fb67075097d73fa1cccbf [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Devang Pateladd58652009-09-23 18:32:25 +000015#include "LLVMContextImpl.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000016#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
Evan Cheng1d9d4bd2009-09-10 04:36:43 +000020#include "llvm/Module.h"
Dan Gohmand2a251f2009-07-17 21:33:58 +000021#include "llvm/Operator.h"
Dan Gohman22571482009-09-03 15:34:35 +000022#include "llvm/Analysis/Dominators.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000023#include "llvm/Support/ErrorHandling.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000024#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000025#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000026#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000027using namespace llvm;
28
Chris Lattner3e13b8c2008-01-02 23:42:30 +000029//===----------------------------------------------------------------------===//
30// CallSite Class
31//===----------------------------------------------------------------------===//
32
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000033User::op_iterator CallSite::getCallee() const {
34 Instruction *II(getInstruction());
35 return isCall()
Gabor Greif9dc154b2010-07-01 10:41:37 +000036 ? (CallInst::ArgOffset
37 ? cast</*FIXME: CallInst*/User>(II)->op_begin()
38 : cast</*FIXME: CallInst*/User>(II)->op_end() - 1)
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000039 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Function
40}
41
Gordon Henriksen14a55692007-12-10 02:14:30 +000042//===----------------------------------------------------------------------===//
43// TerminatorInst Class
44//===----------------------------------------------------------------------===//
45
46// Out of line virtual method, so the vtable, etc has a home.
47TerminatorInst::~TerminatorInst() {
48}
49
Gabor Greiff6caff662008-05-10 08:32:32 +000050//===----------------------------------------------------------------------===//
51// UnaryInstruction Class
52//===----------------------------------------------------------------------===//
53
Gordon Henriksen14a55692007-12-10 02:14:30 +000054// Out of line virtual method, so the vtable, etc has a home.
55UnaryInstruction::~UnaryInstruction() {
56}
57
Chris Lattnerafdb3de2005-01-29 00:35:16 +000058//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000059// SelectInst Class
60//===----------------------------------------------------------------------===//
61
62/// areInvalidOperands - Return a string if the specified operands are invalid
63/// for a select operation, otherwise return null.
64const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
65 if (Op1->getType() != Op2->getType())
66 return "both values to select must have same type";
67
68 if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
69 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000070 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000071 return "vector select condition element type must be i1";
72 const VectorType *ET = dyn_cast<VectorType>(Op1->getType());
73 if (ET == 0)
74 return "selected values for vector select must be vectors";
75 if (ET->getNumElements() != VT->getNumElements())
76 return "vector select requires selected vectors to have "
77 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000078 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000079 return "select condition must be i1 or <n x i1>";
80 }
81 return 0;
82}
83
84
85//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000086// PHINode Class
87//===----------------------------------------------------------------------===//
88
89PHINode::PHINode(const PHINode &PN)
90 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +000091 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +000092 ReservedSpace(PN.getNumOperands()) {
93 Use *OL = OperandList;
94 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +000095 OL[i] = PN.getOperand(i);
96 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +000097 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +000098 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +000099}
100
Gordon Henriksen14a55692007-12-10 02:14:30 +0000101PHINode::~PHINode() {
Chris Lattner7d6aac82008-06-16 04:02:40 +0000102 if (OperandList)
103 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000104}
105
106// removeIncomingValue - Remove an incoming value. This is useful if a
107// predecessor basic block is deleted.
108Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
109 unsigned NumOps = getNumOperands();
110 Use *OL = OperandList;
111 assert(Idx*2 < NumOps && "BB not in PHI node!");
112 Value *Removed = OL[Idx*2];
113
114 // Move everything after this operand down.
115 //
116 // FIXME: we could just swap with the end of the list, then erase. However,
117 // client might not expect this to happen. The code as it is thrashes the
118 // use/def lists, which is kinda lame.
119 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
120 OL[i-2] = OL[i];
121 OL[i-2+1] = OL[i+1];
122 }
123
124 // Nuke the last value.
125 OL[NumOps-2].set(0);
126 OL[NumOps-2+1].set(0);
127 NumOperands = NumOps-2;
128
129 // If the PHI node is dead, because it has zero entries, nuke it now.
130 if (NumOps == 2 && DeletePHIIfEmpty) {
131 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000132 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000133 eraseFromParent();
134 }
135 return Removed;
136}
137
138/// resizeOperands - resize operands - This adjusts the length of the operands
139/// list according to the following behavior:
140/// 1. If NumOps == 0, grow the operand list in response to a push_back style
141/// of operation. This grows the number of ops by 1.5 times.
142/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
143/// 3. If NumOps == NumOperands, trim the reserved space.
144///
145void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000146 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000147 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000148 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000149 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
150 } else if (NumOps*2 > NumOperands) {
151 // No resize needed.
152 if (ReservedSpace >= NumOps) return;
153 } else if (NumOps == NumOperands) {
154 if (ReservedSpace == NumOps) return;
155 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000156 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000157 }
158
159 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000160 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000161 Use *NewOps = allocHungoffUses(NumOps);
Dan Gohman031f0bb2008-06-23 16:45:24 +0000162 std::copy(OldOps, OldOps + e, NewOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000163 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000164 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000165}
166
Nate Begemanb3923212005-08-04 23:24:19 +0000167/// hasConstantValue - If the specified PHI node always merges together the same
168/// value, return the value, otherwise return null.
169///
Dan Gohman22571482009-09-03 15:34:35 +0000170/// If the PHI has undef operands, but all the rest of the operands are
171/// some unique value, return that value if it can be proved that the
172/// value dominates the PHI. If DT is null, use a conservative check,
173/// otherwise use DT to test for dominance.
174///
175Value *PHINode::hasConstantValue(DominatorTree *DT) const {
Chris Lattner7d08da62009-09-21 22:27:34 +0000176 // If the PHI node only has one incoming value, eliminate the PHI node.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000177 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000178 if (getIncomingValue(0) != this) // not X = phi X
179 return getIncomingValue(0);
Chris Lattner7d08da62009-09-21 22:27:34 +0000180 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000181 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000182
Nate Begemanb3923212005-08-04 23:24:19 +0000183 // Otherwise if all of the incoming values are the same for the PHI, replace
184 // the PHI node with the incoming value.
185 //
186 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000187 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000188 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000189 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000190 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000191 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000192 if (InVal && getIncomingValue(i) != InVal)
193 return 0; // Not the same, bail out.
Chris Lattner7d08da62009-09-21 22:27:34 +0000194 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000195 }
Nate Begemanb3923212005-08-04 23:24:19 +0000196
197 // The only case that could cause InVal to be null is if we have a PHI node
198 // that only has entries for itself. In this case, there is no entry into the
199 // loop, so kill the PHI.
200 //
Owen Andersonb292b8c2009-07-30 23:03:37 +0000201 if (InVal == 0) InVal = UndefValue::get(getType());
Nate Begemanb3923212005-08-04 23:24:19 +0000202
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000203 // If we have a PHI node like phi(X, undef, X), where X is defined by some
204 // instruction, we cannot always return X as the result of the PHI node. Only
205 // do this if X is not an instruction (thus it must dominate the PHI block),
206 // or if the client is prepared to deal with this possibility.
Chris Lattner7d08da62009-09-21 22:27:34 +0000207 if (!HasUndefInput || !isa<Instruction>(InVal))
208 return InVal;
209
210 Instruction *IV = cast<Instruction>(InVal);
211 if (DT) {
212 // We have a DominatorTree. Do a precise test.
213 if (!DT->dominates(IV, this))
214 return 0;
215 } else {
216 // If it is in the entry block, it obviously dominates everything.
217 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
218 isa<InvokeInst>(IV))
219 return 0; // Cannot guarantee that InVal dominates this PHINode.
220 }
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000221
Nate Begemanb3923212005-08-04 23:24:19 +0000222 // All of the incoming values are the same, return the value now.
223 return InVal;
224}
225
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000226
227//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000228// CallInst Implementation
229//===----------------------------------------------------------------------===//
230
Gordon Henriksen14a55692007-12-10 02:14:30 +0000231CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000232}
233
Chris Lattner054ba2c2007-02-13 00:58:44 +0000234void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000235 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
Gabor Greifeec74582010-06-29 11:41:38 +0000236 Op<ArgOffset -1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000237
Misha Brukmanb1c93172005-04-21 23:48:37 +0000238 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000239 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000240 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000241
Chris Lattner054ba2c2007-02-13 00:58:44 +0000242 assert((NumParams == FTy->getNumParams() ||
243 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000244 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000245 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000246 assert((i >= FTy->getNumParams() ||
247 FTy->getParamType(i) == Params[i]->getType()) &&
248 "Calling a function with a bad signature!");
Gabor Greifeec74582010-06-29 11:41:38 +0000249 OperandList[i + ArgOffset] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000250 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000251}
252
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000253void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000254 assert(NumOperands == 3 && "NumOperands not set up?");
Gabor Greifeec74582010-06-29 11:41:38 +0000255 Op<ArgOffset -1>() = Func;
256 Op<ArgOffset + 0>() = Actual1;
257 Op<ArgOffset + 1>() = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000258
259 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000260 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000261 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000262
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000263 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000264 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000265 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000266 assert((0 >= FTy->getNumParams() ||
267 FTy->getParamType(0) == Actual1->getType()) &&
268 "Calling a function with a bad signature!");
269 assert((1 >= FTy->getNumParams() ||
270 FTy->getParamType(1) == Actual2->getType()) &&
271 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000272}
273
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000274void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000275 assert(NumOperands == 2 && "NumOperands not set up?");
Gabor Greifeec74582010-06-29 11:41:38 +0000276 Op<ArgOffset -1>() = Func;
277 Op<ArgOffset + 0>() = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000278
279 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000280 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000281 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000282
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000283 assert((FTy->getNumParams() == 1 ||
284 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000285 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000286 assert((0 == FTy->getNumParams() ||
287 FTy->getParamType(0) == Actual->getType()) &&
288 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000289}
290
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000291void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000292 assert(NumOperands == 1 && "NumOperands not set up?");
Gabor Greifeec74582010-06-29 11:41:38 +0000293 Op<ArgOffset -1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000294
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000295 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000296 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000297 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000298
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000299 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000300}
301
Daniel Dunbar4975db62009-07-25 04:41:11 +0000302CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000303 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000304 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
305 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000306 Instruction::Call,
307 OperandTraits<CallInst>::op_end(this) - 2,
308 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000309 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000310 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000311}
312
Daniel Dunbar4975db62009-07-25 04:41:11 +0000313CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000314 BasicBlock *InsertAtEnd)
315 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
316 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000317 Instruction::Call,
318 OperandTraits<CallInst>::op_end(this) - 2,
319 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000320 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000321 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000322}
Daniel Dunbar4975db62009-07-25 04:41:11 +0000323CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000324 Instruction *InsertBefore)
325 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
326 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000327 Instruction::Call,
328 OperandTraits<CallInst>::op_end(this) - 1,
329 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000330 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000331 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000332}
333
Daniel Dunbar4975db62009-07-25 04:41:11 +0000334CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000335 BasicBlock *InsertAtEnd)
336 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
337 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000338 Instruction::Call,
339 OperandTraits<CallInst>::op_end(this) - 1,
340 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000341 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000342 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000343}
344
Misha Brukmanb1c93172005-04-21 23:48:37 +0000345CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000346 : Instruction(CI.getType(), Instruction::Call,
347 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000348 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000349 setAttributes(CI.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000350 setTailCall(CI.isTailCall());
351 setCallingConv(CI.getCallingConv());
352
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000353 Use *OL = OperandList;
354 Use *InOL = CI.OperandList;
355 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000356 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000357 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000358}
359
Devang Patel4c758ea2008-09-25 21:00:45 +0000360void CallInst::addAttribute(unsigned i, Attributes attr) {
361 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000362 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000363 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000364}
365
Devang Patel4c758ea2008-09-25 21:00:45 +0000366void CallInst::removeAttribute(unsigned i, Attributes attr) {
367 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000368 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000369 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000370}
371
Devang Patelba3fa6c2008-09-23 23:03:40 +0000372bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000373 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000374 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000375 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000376 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000377 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000378}
379
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000380/// IsConstantOne - Return true only if val is constant int 1
381static bool IsConstantOne(Value *val) {
382 assert(val && "IsConstantOne does not work with NULL val");
383 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
384}
385
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000386static Instruction *createMalloc(Instruction *InsertBefore,
387 BasicBlock *InsertAtEnd, const Type *IntPtrTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000388 const Type *AllocTy, Value *AllocSize,
389 Value *ArraySize, Function *MallocF,
390 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000391 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000392 "createMalloc needs either InsertBefore or InsertAtEnd");
393
394 // malloc(type) becomes:
395 // bitcast (i8* malloc(typeSize)) to type*
396 // malloc(type, arraySize) becomes:
397 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000398 if (!ArraySize)
399 ArraySize = ConstantInt::get(IntPtrTy, 1);
400 else if (ArraySize->getType() != IntPtrTy) {
401 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000402 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
403 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000404 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000405 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
406 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000407 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000408
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000409 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000410 if (IsConstantOne(AllocSize)) {
411 AllocSize = ArraySize; // Operand * 1 = Operand
412 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
413 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
414 false /*ZExt*/);
415 // Malloc arg is constant product of type size and array size
416 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
417 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000418 // Multiply type size by the array size...
419 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000420 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
421 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000422 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000423 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
424 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000425 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000426 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000427
Victor Hernandez788eaab2009-09-18 19:20:02 +0000428 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000429 // Create the call to Malloc.
430 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
431 Module* M = BB->getParent()->getParent();
Duncan Sands9ed7b162009-10-06 15:40:36 +0000432 const Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000433 Value *MallocFunc = MallocF;
434 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000435 // prototype malloc as "void *malloc(size_t)"
Victor Hernandezbb336a12009-11-10 19:53:28 +0000436 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000437 const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000438 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000439 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000440 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000441 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000442 Result = MCall;
443 if (Result->getType() != AllocPtrType)
444 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000445 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000446 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000447 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000448 Result = MCall;
449 if (Result->getType() != AllocPtrType) {
450 InsertAtEnd->getInstList().push_back(MCall);
451 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000452 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000453 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000454 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000455 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000456 if (Function *F = dyn_cast<Function>(MallocFunc)) {
457 MCall->setCallingConv(F->getCallingConv());
458 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
459 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000460 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000461
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000462 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000463}
464
465/// CreateMalloc - Generate the IR for a call to malloc:
466/// 1. Compute the malloc call's argument as the specified type's size,
467/// possibly multiplied by the array size if the array size is not
468/// constant 1.
469/// 2. Call malloc with that argument.
470/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000471Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
472 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000473 Value *AllocSize, Value *ArraySize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000474 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000475 const Twine &Name) {
476 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000477 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000478}
479
480/// CreateMalloc - Generate the IR for a call to malloc:
481/// 1. Compute the malloc call's argument as the specified type's size,
482/// possibly multiplied by the array size if the array size is not
483/// constant 1.
484/// 2. Call malloc with that argument.
485/// 3. Bitcast the result of the malloc call to the specified type.
486/// Note: This function does not add the bitcast to the basic block, that is the
487/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000488Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
489 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000490 Value *AllocSize, Value *ArraySize,
491 Function *MallocF, const Twine &Name) {
492 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000493 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000494}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000495
Victor Hernandeze2971492009-10-24 04:23:03 +0000496static Instruction* createFree(Value* Source, Instruction *InsertBefore,
497 BasicBlock *InsertAtEnd) {
498 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
499 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000500 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000501 "Can not free something of nonpointer type!");
502
503 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
504 Module* M = BB->getParent()->getParent();
505
506 const Type *VoidTy = Type::getVoidTy(M->getContext());
507 const Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
508 // prototype free as "void free(void*)"
Chris Lattner2156c222009-11-09 07:12:01 +0000509 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000510 CallInst* Result = NULL;
511 Value *PtrCast = Source;
512 if (InsertBefore) {
513 if (Source->getType() != IntPtrTy)
514 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
515 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
516 } else {
517 if (Source->getType() != IntPtrTy)
518 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
519 Result = CallInst::Create(FreeFunc, PtrCast, "");
520 }
521 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000522 if (Function *F = dyn_cast<Function>(FreeFunc))
523 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000524
525 return Result;
526}
527
528/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000529Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
530 return createFree(Source, InsertBefore, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000531}
532
533/// CreateFree - Generate the IR for a call to the builtin free function.
534/// Note: This function does not add the call to the basic block, that is the
535/// responsibility of the caller.
536Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
537 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
538 assert(FreeCall && "CreateFree did not create a CallInst");
539 return FreeCall;
540}
541
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000542//===----------------------------------------------------------------------===//
543// InvokeInst Implementation
544//===----------------------------------------------------------------------===//
545
546void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000547 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000548 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000549 Op<-3>() = Fn;
550 Op<-2>() = IfNormal;
551 Op<-1>() = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000552 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000553 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000554 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000555
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000556 assert(((NumArgs == FTy->getNumParams()) ||
557 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000558 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000559
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000560 Use *OL = OperandList;
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000561 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000562 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000563 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000564 "Invoking a function with a bad signature!");
565
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000566 OL[i] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000567 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000568}
569
Misha Brukmanb1c93172005-04-21 23:48:37 +0000570InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000571 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000572 OperandTraits<InvokeInst>::op_end(this)
573 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000574 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000575 setAttributes(II.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000576 setCallingConv(II.getCallingConv());
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000577 Use *OL = OperandList, *InOL = II.OperandList;
578 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000579 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000580 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000581}
582
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000583BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
584 return getSuccessor(idx);
585}
586unsigned InvokeInst::getNumSuccessorsV() const {
587 return getNumSuccessors();
588}
589void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
590 return setSuccessor(idx, B);
591}
592
Devang Patelba3fa6c2008-09-23 23:03:40 +0000593bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000594 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000595 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000596 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000597 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000598 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000599}
600
Devang Patel4c758ea2008-09-25 21:00:45 +0000601void InvokeInst::addAttribute(unsigned i, Attributes attr) {
602 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000603 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000604 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000605}
606
Devang Patel4c758ea2008-09-25 21:00:45 +0000607void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
608 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000609 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000610 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000611}
612
Duncan Sands5208d1a2007-11-28 17:07:01 +0000613
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000614//===----------------------------------------------------------------------===//
615// ReturnInst Implementation
616//===----------------------------------------------------------------------===//
617
Chris Lattner2195fc42007-02-24 00:55:48 +0000618ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000619 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000620 OperandTraits<ReturnInst>::op_end(this) -
621 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000622 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000623 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000624 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000625 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000626}
627
Owen Anderson55f1c092009-08-13 21:58:54 +0000628ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
629 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000630 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
631 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000632 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000633 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000634}
Owen Anderson55f1c092009-08-13 21:58:54 +0000635ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
636 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000637 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
638 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000639 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000640 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000641}
Owen Anderson55f1c092009-08-13 21:58:54 +0000642ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
643 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000644 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000645}
646
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000647unsigned ReturnInst::getNumSuccessorsV() const {
648 return getNumSuccessors();
649}
650
Devang Patelae682fb2008-02-26 17:56:20 +0000651/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
652/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000653void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000654 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000655}
656
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000657BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000658 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000659 return 0;
660}
661
Devang Patel59643e52008-02-23 00:35:18 +0000662ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000663}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000664
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000665//===----------------------------------------------------------------------===//
666// UnwindInst Implementation
667//===----------------------------------------------------------------------===//
668
Owen Anderson55f1c092009-08-13 21:58:54 +0000669UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
670 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
671 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000672}
Owen Anderson55f1c092009-08-13 21:58:54 +0000673UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
674 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
675 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000676}
677
678
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000679unsigned UnwindInst::getNumSuccessorsV() const {
680 return getNumSuccessors();
681}
682
683void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000684 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000685}
686
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000687BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000688 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000689 return 0;
690}
691
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000692//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000693// UnreachableInst Implementation
694//===----------------------------------------------------------------------===//
695
Owen Anderson55f1c092009-08-13 21:58:54 +0000696UnreachableInst::UnreachableInst(LLVMContext &Context,
697 Instruction *InsertBefore)
698 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
699 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000700}
Owen Anderson55f1c092009-08-13 21:58:54 +0000701UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
702 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
703 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000704}
705
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000706unsigned UnreachableInst::getNumSuccessorsV() const {
707 return getNumSuccessors();
708}
709
710void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000711 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000712}
713
714BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000715 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000716 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000717}
718
719//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000720// BranchInst Implementation
721//===----------------------------------------------------------------------===//
722
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000723void BranchInst::AssertOK() {
724 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000725 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000726 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000727}
728
Chris Lattner2195fc42007-02-24 00:55:48 +0000729BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000730 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000731 OperandTraits<BranchInst>::op_end(this) - 1,
732 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000733 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000734 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000735}
736BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
737 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000738 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000739 OperandTraits<BranchInst>::op_end(this) - 3,
740 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000741 Op<-1>() = IfTrue;
742 Op<-2>() = IfFalse;
743 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000744#ifndef NDEBUG
745 AssertOK();
746#endif
747}
748
749BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000750 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000751 OperandTraits<BranchInst>::op_end(this) - 1,
752 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000753 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000754 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000755}
756
757BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
758 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000759 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000760 OperandTraits<BranchInst>::op_end(this) - 3,
761 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000762 Op<-1>() = IfTrue;
763 Op<-2>() = IfFalse;
764 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000765#ifndef NDEBUG
766 AssertOK();
767#endif
768}
769
770
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000771BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000772 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000773 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
774 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000775 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000776 if (BI.getNumOperands() != 1) {
777 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000778 Op<-3>() = BI.Op<-3>();
779 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000780 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000781 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000782}
783
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000784
785Use* Use::getPrefix() {
786 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
787 if (PotentialPrefix.getOpaqueValue())
788 return 0;
789
790 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
791}
792
793BranchInst::~BranchInst() {
794 if (NumOperands == 1) {
795 if (Use *Prefix = OperandList->getPrefix()) {
796 Op<-1>() = 0;
797 //
798 // mark OperandList to have a special value for scrutiny
799 // by baseclass destructors and operator delete
800 OperandList = Prefix;
801 } else {
802 NumOperands = 3;
803 OperandList = op_begin();
804 }
805 }
806}
807
808
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000809BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
810 return getSuccessor(idx);
811}
812unsigned BranchInst::getNumSuccessorsV() const {
813 return getNumSuccessors();
814}
815void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
816 setSuccessor(idx, B);
817}
818
819
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000820//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000821// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000822//===----------------------------------------------------------------------===//
823
Owen Andersonb6b25302009-07-14 23:09:55 +0000824static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000825 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000826 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000827 else {
828 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000829 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000830 assert(Amt->getType()->isIntegerTy() &&
831 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000832 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000833 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000834}
835
Victor Hernandez8acf2952009-10-23 21:09:37 +0000836AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
837 const Twine &Name, Instruction *InsertBefore)
838 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
839 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
840 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000841 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000842 setName(Name);
843}
844
845AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
846 const Twine &Name, BasicBlock *InsertAtEnd)
847 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
848 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
849 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000850 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000851 setName(Name);
852}
853
854AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
855 Instruction *InsertBefore)
856 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
857 getAISize(Ty->getContext(), 0), InsertBefore) {
858 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000859 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000860 setName(Name);
861}
862
863AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
864 BasicBlock *InsertAtEnd)
865 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
866 getAISize(Ty->getContext(), 0), InsertAtEnd) {
867 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000868 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000869 setName(Name);
870}
871
872AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
873 const Twine &Name, Instruction *InsertBefore)
874 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000875 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000876 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000877 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000878 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000879}
880
Victor Hernandez8acf2952009-10-23 21:09:37 +0000881AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
882 const Twine &Name, BasicBlock *InsertAtEnd)
883 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000884 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000885 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000886 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000887 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000888}
889
Gordon Henriksen14a55692007-12-10 02:14:30 +0000890// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000891AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000892}
893
Victor Hernandez8acf2952009-10-23 21:09:37 +0000894void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000895 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000896 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohmanaa583d72008-03-24 16:55:58 +0000897 assert(getAlignment() == Align && "Alignment representation error!");
898}
899
Victor Hernandez8acf2952009-10-23 21:09:37 +0000900bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000901 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
902 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000903 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000904}
905
Victor Hernandez8acf2952009-10-23 21:09:37 +0000906const Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000907 return getType()->getElementType();
908}
909
Chris Lattner8b291e62008-11-26 02:54:17 +0000910/// isStaticAlloca - Return true if this alloca is in the entry block of the
911/// function and is a constant size. If so, the code generator will fold it
912/// into the prolog/epilog code, so it is basically free.
913bool AllocaInst::isStaticAlloca() const {
914 // Must be constant size.
915 if (!isa<ConstantInt>(getArraySize())) return false;
916
917 // Must be in the entry block.
918 const BasicBlock *Parent = getParent();
919 return Parent == &Parent->getParent()->front();
920}
921
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000922//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000923// LoadInst Implementation
924//===----------------------------------------------------------------------===//
925
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000926void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +0000927 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000928 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000929}
930
Daniel Dunbar4975db62009-07-25 04:41:11 +0000931LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000932 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000933 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000934 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000935 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000936 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000937 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000938}
939
Daniel Dunbar4975db62009-07-25 04:41:11 +0000940LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000941 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000942 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000943 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000944 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000945 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000946 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000947}
948
Daniel Dunbar4975db62009-07-25 04:41:11 +0000949LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000950 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000951 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000952 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000953 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000954 setAlignment(0);
955 AssertOK();
956 setName(Name);
957}
958
Daniel Dunbar4975db62009-07-25 04:41:11 +0000959LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000960 unsigned Align, Instruction *InsertBef)
961 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
962 Load, Ptr, InsertBef) {
963 setVolatile(isVolatile);
964 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000965 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000966 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000967}
968
Daniel Dunbar4975db62009-07-25 04:41:11 +0000969LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000970 unsigned Align, BasicBlock *InsertAE)
971 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
972 Load, Ptr, InsertAE) {
973 setVolatile(isVolatile);
974 setAlignment(Align);
975 AssertOK();
976 setName(Name);
977}
978
Daniel Dunbar4975db62009-07-25 04:41:11 +0000979LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000980 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000981 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000982 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000983 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000984 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000985 AssertOK();
986 setName(Name);
987}
988
Daniel Dunbar27096822009-08-11 18:11:15 +0000989
990
991LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
992 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
993 Load, Ptr, InsertBef) {
994 setVolatile(false);
995 setAlignment(0);
996 AssertOK();
997 if (Name && Name[0]) setName(Name);
998}
999
1000LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1001 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1002 Load, Ptr, InsertAE) {
1003 setVolatile(false);
1004 setAlignment(0);
1005 AssertOK();
1006 if (Name && Name[0]) setName(Name);
1007}
1008
1009LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1010 Instruction *InsertBef)
1011: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1012 Load, Ptr, InsertBef) {
1013 setVolatile(isVolatile);
1014 setAlignment(0);
1015 AssertOK();
1016 if (Name && Name[0]) setName(Name);
1017}
1018
1019LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1020 BasicBlock *InsertAE)
1021 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1022 Load, Ptr, InsertAE) {
1023 setVolatile(isVolatile);
1024 setAlignment(0);
1025 AssertOK();
1026 if (Name && Name[0]) setName(Name);
1027}
1028
Christopher Lamb84485702007-04-22 19:24:39 +00001029void LoadInst::setAlignment(unsigned Align) {
1030 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001031 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1032 ((Log2_32(Align)+1)<<1));
Christopher Lamb84485702007-04-22 19:24:39 +00001033}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001034
1035//===----------------------------------------------------------------------===//
1036// StoreInst Implementation
1037//===----------------------------------------------------------------------===//
1038
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001039void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001040 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001041 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001042 "Ptr must have pointer type!");
1043 assert(getOperand(0)->getType() ==
1044 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001045 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001046}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001047
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001048
1049StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001050 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001051 OperandTraits<StoreInst>::op_begin(this),
1052 OperandTraits<StoreInst>::operands(this),
1053 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001054 Op<0>() = val;
1055 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001056 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001057 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001058 AssertOK();
1059}
1060
1061StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001062 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001063 OperandTraits<StoreInst>::op_begin(this),
1064 OperandTraits<StoreInst>::operands(this),
1065 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001066 Op<0>() = val;
1067 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001068 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001069 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001070 AssertOK();
1071}
1072
Misha Brukmanb1c93172005-04-21 23:48:37 +00001073StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001074 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001075 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001076 OperandTraits<StoreInst>::op_begin(this),
1077 OperandTraits<StoreInst>::operands(this),
1078 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001079 Op<0>() = val;
1080 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001081 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001082 setAlignment(0);
1083 AssertOK();
1084}
1085
1086StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1087 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001088 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001089 OperandTraits<StoreInst>::op_begin(this),
1090 OperandTraits<StoreInst>::operands(this),
1091 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001092 Op<0>() = val;
1093 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001094 setVolatile(isVolatile);
1095 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001096 AssertOK();
1097}
1098
Misha Brukmanb1c93172005-04-21 23:48:37 +00001099StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001100 unsigned Align, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001101 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001102 OperandTraits<StoreInst>::op_begin(this),
1103 OperandTraits<StoreInst>::operands(this),
1104 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001105 Op<0>() = val;
1106 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001107 setVolatile(isVolatile);
1108 setAlignment(Align);
1109 AssertOK();
1110}
1111
1112StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001113 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001114 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001115 OperandTraits<StoreInst>::op_begin(this),
1116 OperandTraits<StoreInst>::operands(this),
1117 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001118 Op<0>() = val;
1119 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001120 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001121 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001122 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001123}
1124
Christopher Lamb84485702007-04-22 19:24:39 +00001125void StoreInst::setAlignment(unsigned Align) {
1126 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001127 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1128 ((Log2_32(Align)+1) << 1));
Christopher Lamb84485702007-04-22 19:24:39 +00001129}
1130
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001131//===----------------------------------------------------------------------===//
1132// GetElementPtrInst Implementation
1133//===----------------------------------------------------------------------===//
1134
Christopher Lambedf07882007-12-17 01:12:55 +00001135static unsigned retrieveAddrSpace(const Value *Val) {
1136 return cast<PointerType>(Val->getType())->getAddressSpace();
1137}
1138
Gabor Greif21ba1842008-06-06 20:28:12 +00001139void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001140 const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001141 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1142 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001143 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001144
Chris Lattner79807c3d2007-01-31 19:47:18 +00001145 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001146 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001147
1148 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001149}
1150
Daniel Dunbar4975db62009-07-25 04:41:11 +00001151void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001152 assert(NumOperands == 2 && "NumOperands not initialized?");
1153 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001154 OL[0] = Ptr;
1155 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001156
1157 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001158}
1159
Gabor Greiff6caff662008-05-10 08:32:32 +00001160GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001161 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001162 OperandTraits<GetElementPtrInst>::op_end(this)
1163 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001164 GEPI.getNumOperands()) {
1165 Use *OL = OperandList;
1166 Use *GEPIOL = GEPI.OperandList;
1167 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001168 OL[i] = GEPIOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001169 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001170}
1171
Chris Lattner82981202005-05-03 05:43:30 +00001172GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001173 const Twine &Name, Instruction *InBe)
Owen Anderson4056ca92009-07-29 22:17:13 +00001174 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001175 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001176 GetElementPtr,
1177 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1178 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001179 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001180}
1181
1182GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001183 const Twine &Name, BasicBlock *IAE)
Owen Anderson4056ca92009-07-29 22:17:13 +00001184 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001185 checkType(getIndexedType(Ptr->getType(),Idx)),
1186 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001187 GetElementPtr,
1188 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1189 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001190 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001191}
1192
Chris Lattner6090a422009-03-09 04:46:40 +00001193/// getIndexedType - Returns the type of the element that would be accessed with
1194/// a gep instruction with the specified parameters.
1195///
1196/// The Idxs pointer should point to a continuous piece of memory containing the
1197/// indices, either as Value* or uint64_t.
1198///
1199/// A null type is returned if the indices are invalid for the specified
1200/// pointer type.
1201///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001202template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001203static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1204 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001205 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1206 if (!PTy) return 0; // Type isn't a pointer type!
1207 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001208
Chris Lattner6090a422009-03-09 04:46:40 +00001209 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001210 if (NumIdx == 0)
1211 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001212
1213 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001214 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1215 // that contain opaque types) under the assumption that it will be resolved to
1216 // a sane type later.
1217 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001218 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001219
Dan Gohman1ecaf452008-05-31 00:58:22 +00001220 unsigned CurIdx = 1;
1221 for (; CurIdx != NumIdx; ++CurIdx) {
1222 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001223 if (!CT || CT->isPointerTy()) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001224 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001225 if (!CT->indexValid(Index)) return 0;
1226 Agg = CT->getTypeAtIndex(Index);
1227
1228 // If the new type forwards to another type, then it is in the middle
1229 // of being refined to another type (and hence, may have dropped all
1230 // references to what it was using before). So, use the new forwarded
1231 // type.
1232 if (const Type *Ty = Agg->getForwardedType())
1233 Agg = Ty;
1234 }
1235 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001236}
1237
Matthijs Kooijman04468622008-07-29 08:46:11 +00001238const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1239 Value* const *Idxs,
1240 unsigned NumIdx) {
1241 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1242}
1243
1244const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1245 uint64_t const *Idxs,
1246 unsigned NumIdx) {
1247 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1248}
1249
Chris Lattner82981202005-05-03 05:43:30 +00001250const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1251 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1252 if (!PTy) return 0; // Type isn't a pointer type!
1253
1254 // Check the pointer index.
1255 if (!PTy->indexValid(Idx)) return 0;
1256
Chris Lattnerc2233332005-05-03 16:44:45 +00001257 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001258}
1259
Chris Lattner45f15572007-04-14 00:12:57 +00001260
1261/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1262/// zeros. If so, the result pointer and the first operand have the same
1263/// value, just potentially different types.
1264bool GetElementPtrInst::hasAllZeroIndices() const {
1265 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1266 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1267 if (!CI->isZero()) return false;
1268 } else {
1269 return false;
1270 }
1271 }
1272 return true;
1273}
1274
Chris Lattner27058292007-04-27 20:35:56 +00001275/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1276/// constant integers. If so, the result pointer and the first operand have
1277/// a constant offset between them.
1278bool GetElementPtrInst::hasAllConstantIndices() const {
1279 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1280 if (!isa<ConstantInt>(getOperand(i)))
1281 return false;
1282 }
1283 return true;
1284}
1285
Dan Gohman1b849082009-09-07 23:54:19 +00001286void GetElementPtrInst::setIsInBounds(bool B) {
1287 cast<GEPOperator>(this)->setIsInBounds(B);
1288}
Chris Lattner45f15572007-04-14 00:12:57 +00001289
Nick Lewycky28a5f252009-09-27 21:33:04 +00001290bool GetElementPtrInst::isInBounds() const {
1291 return cast<GEPOperator>(this)->isInBounds();
1292}
1293
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001294//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001295// ExtractElementInst Implementation
1296//===----------------------------------------------------------------------===//
1297
1298ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001299 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001300 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001301 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001302 ExtractElement,
1303 OperandTraits<ExtractElementInst>::op_begin(this),
1304 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001305 assert(isValidOperands(Val, Index) &&
1306 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001307 Op<0>() = Val;
1308 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001309 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001310}
1311
1312ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001313 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001314 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001315 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001316 ExtractElement,
1317 OperandTraits<ExtractElementInst>::op_begin(this),
1318 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001319 assert(isValidOperands(Val, Index) &&
1320 "Invalid extractelement instruction operands!");
1321
Gabor Greif2d3024d2008-05-26 21:33:52 +00001322 Op<0>() = Val;
1323 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001324 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001325}
1326
Chris Lattner65511ff2006-10-05 06:24:58 +00001327
Chris Lattner54865b32006-04-08 04:05:48 +00001328bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001329 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
Chris Lattner54865b32006-04-08 04:05:48 +00001330 return false;
1331 return true;
1332}
1333
1334
Robert Bocchino23004482006-01-10 19:05:34 +00001335//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001336// InsertElementInst Implementation
1337//===----------------------------------------------------------------------===//
1338
Chris Lattner54865b32006-04-08 04:05:48 +00001339InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001340 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001341 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001342 : Instruction(Vec->getType(), InsertElement,
1343 OperandTraits<InsertElementInst>::op_begin(this),
1344 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001345 assert(isValidOperands(Vec, Elt, Index) &&
1346 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001347 Op<0>() = Vec;
1348 Op<1>() = Elt;
1349 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001350 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001351}
1352
Chris Lattner54865b32006-04-08 04:05:48 +00001353InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001354 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001355 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001356 : Instruction(Vec->getType(), InsertElement,
1357 OperandTraits<InsertElementInst>::op_begin(this),
1358 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001359 assert(isValidOperands(Vec, Elt, Index) &&
1360 "Invalid insertelement instruction operands!");
1361
Gabor Greif2d3024d2008-05-26 21:33:52 +00001362 Op<0>() = Vec;
1363 Op<1>() = Elt;
1364 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001365 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001366}
1367
Chris Lattner54865b32006-04-08 04:05:48 +00001368bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1369 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001370 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001371 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001372
Reid Spencerd84d35b2007-02-15 02:26:10 +00001373 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001374 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001375
Duncan Sands9dff9be2010-02-15 16:12:20 +00001376 if (!Index->getType()->isIntegerTy(32))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001377 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001378 return true;
1379}
1380
1381
Robert Bocchinoca27f032006-01-17 20:07:22 +00001382//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001383// ShuffleVectorInst Implementation
1384//===----------------------------------------------------------------------===//
1385
1386ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001387 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001388 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001389: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001390 cast<VectorType>(Mask->getType())->getNumElements()),
1391 ShuffleVector,
1392 OperandTraits<ShuffleVectorInst>::op_begin(this),
1393 OperandTraits<ShuffleVectorInst>::operands(this),
1394 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001395 assert(isValidOperands(V1, V2, Mask) &&
1396 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001397 Op<0>() = V1;
1398 Op<1>() = V2;
1399 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001400 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001401}
1402
1403ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001404 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001405 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001406: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1407 cast<VectorType>(Mask->getType())->getNumElements()),
1408 ShuffleVector,
1409 OperandTraits<ShuffleVectorInst>::op_begin(this),
1410 OperandTraits<ShuffleVectorInst>::operands(this),
1411 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001412 assert(isValidOperands(V1, V2, Mask) &&
1413 "Invalid shuffle vector instruction operands!");
1414
Gabor Greif2d3024d2008-05-26 21:33:52 +00001415 Op<0>() = V1;
1416 Op<1>() = V2;
1417 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001418 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001419}
1420
Mon P Wang25f01062008-11-10 04:46:22 +00001421bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001422 const Value *Mask) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001423 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001424 return false;
1425
1426 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1427 if (!isa<Constant>(Mask) || MaskTy == 0 ||
Duncan Sands9dff9be2010-02-15 16:12:20 +00001428 !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001429 return false;
1430 return true;
1431}
1432
Chris Lattnerf724e342008-03-02 05:28:33 +00001433/// getMaskValue - Return the index from the shuffle mask for the specified
1434/// output result. This is either -1 if the element is undef or a number less
1435/// than 2*numelements.
1436int ShuffleVectorInst::getMaskValue(unsigned i) const {
1437 const Constant *Mask = cast<Constant>(getOperand(2));
1438 if (isa<UndefValue>(Mask)) return -1;
1439 if (isa<ConstantAggregateZero>(Mask)) return 0;
1440 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1441 assert(i < MaskCV->getNumOperands() && "Index out of range");
1442
1443 if (isa<UndefValue>(MaskCV->getOperand(i)))
1444 return -1;
1445 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1446}
1447
Dan Gohman12fce772008-05-15 19:50:34 +00001448//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001449// InsertValueInst Class
1450//===----------------------------------------------------------------------===//
1451
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001452void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001453 unsigned NumIdx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001454 assert(NumOperands == 2 && "NumOperands not initialized?");
1455 Op<0>() = Agg;
1456 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001457
Dan Gohmandd41bba2010-06-21 19:47:52 +00001458 Indices.append(Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001459 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001460}
1461
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001462void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001463 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001464 assert(NumOperands == 2 && "NumOperands not initialized?");
1465 Op<0>() = Agg;
1466 Op<1>() = Val;
1467
1468 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001469 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001470}
1471
1472InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001473 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001474 OperandTraits<InsertValueInst>::op_begin(this), 2),
1475 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001476 Op<0>() = IVI.getOperand(0);
1477 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001478 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001479}
1480
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001481InsertValueInst::InsertValueInst(Value *Agg,
1482 Value *Val,
1483 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001484 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001485 Instruction *InsertBefore)
1486 : Instruction(Agg->getType(), InsertValue,
1487 OperandTraits<InsertValueInst>::op_begin(this),
1488 2, InsertBefore) {
1489 init(Agg, Val, Idx, Name);
1490}
1491
1492InsertValueInst::InsertValueInst(Value *Agg,
1493 Value *Val,
1494 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001495 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001496 BasicBlock *InsertAtEnd)
1497 : Instruction(Agg->getType(), InsertValue,
1498 OperandTraits<InsertValueInst>::op_begin(this),
1499 2, InsertAtEnd) {
1500 init(Agg, Val, Idx, Name);
1501}
1502
Dan Gohman0752bff2008-05-23 00:36:11 +00001503//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001504// ExtractValueInst Class
1505//===----------------------------------------------------------------------===//
1506
Gabor Greifcbcc4952008-06-06 21:06:32 +00001507void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001508 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001509 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001510
Dan Gohmandd41bba2010-06-21 19:47:52 +00001511 Indices.append(Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001512 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001513}
1514
Daniel Dunbar4975db62009-07-25 04:41:11 +00001515void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001516 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001517
1518 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001519 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001520}
1521
1522ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001523 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001524 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001525 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001526}
1527
Dan Gohman12fce772008-05-15 19:50:34 +00001528// getIndexedType - Returns the type of the element that would be extracted
1529// with an extractvalue instruction with the specified parameters.
1530//
1531// A null type is returned if the indices are invalid for the specified
1532// pointer type.
1533//
1534const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001535 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001536 unsigned NumIdx) {
1537 unsigned CurIdx = 0;
1538 for (; CurIdx != NumIdx; ++CurIdx) {
1539 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001540 if (!CT || CT->isPointerTy() || CT->isVectorTy()) return 0;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001541 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001542 if (!CT->indexValid(Index)) return 0;
1543 Agg = CT->getTypeAtIndex(Index);
1544
1545 // If the new type forwards to another type, then it is in the middle
1546 // of being refined to another type (and hence, may have dropped all
1547 // references to what it was using before). So, use the new forwarded
1548 // type.
1549 if (const Type *Ty = Agg->getForwardedType())
1550 Agg = Ty;
1551 }
1552 return CurIdx == NumIdx ? Agg : 0;
1553}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001554
Dan Gohman4a3125b2008-06-17 21:07:55 +00001555const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001556 unsigned Idx) {
1557 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001558}
1559
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001560//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001561// BinaryOperator Class
1562//===----------------------------------------------------------------------===//
1563
Chris Lattner2195fc42007-02-24 00:55:48 +00001564BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001565 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001566 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001567 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001568 OperandTraits<BinaryOperator>::op_begin(this),
1569 OperandTraits<BinaryOperator>::operands(this),
1570 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001571 Op<0>() = S1;
1572 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001573 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001574 setName(Name);
1575}
1576
1577BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001578 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001579 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001580 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001581 OperandTraits<BinaryOperator>::op_begin(this),
1582 OperandTraits<BinaryOperator>::operands(this),
1583 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001584 Op<0>() = S1;
1585 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001586 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001587 setName(Name);
1588}
1589
1590
1591void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001592 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001593 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001594 assert(LHS->getType() == RHS->getType() &&
1595 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001596#ifndef NDEBUG
1597 switch (iType) {
1598 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001599 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001600 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001601 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001602 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001603 "Tried to create an integer operation on a non-integer type!");
1604 break;
1605 case FAdd: case FSub:
1606 case FMul:
1607 assert(getType() == LHS->getType() &&
1608 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001609 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001610 "Tried to create a floating-point operation on a "
1611 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001612 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001613 case UDiv:
1614 case SDiv:
1615 assert(getType() == LHS->getType() &&
1616 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001617 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001618 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001619 "Incorrect operand type (not integer) for S/UDIV");
1620 break;
1621 case FDiv:
1622 assert(getType() == LHS->getType() &&
1623 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001624 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001625 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001626 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001627 case URem:
1628 case SRem:
1629 assert(getType() == LHS->getType() &&
1630 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001631 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001632 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001633 "Incorrect operand type (not integer) for S/UREM");
1634 break;
1635 case FRem:
1636 assert(getType() == LHS->getType() &&
1637 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001638 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001639 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001640 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001641 case Shl:
1642 case LShr:
1643 case AShr:
1644 assert(getType() == LHS->getType() &&
1645 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001646 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001647 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001648 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001649 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001650 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001651 case And: case Or:
1652 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001653 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001654 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001655 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001656 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001657 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001658 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001659 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001660 default:
1661 break;
1662 }
1663#endif
1664}
1665
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001666BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001667 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001668 Instruction *InsertBefore) {
1669 assert(S1->getType() == S2->getType() &&
1670 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001671 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001672}
1673
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001674BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001675 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001676 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001677 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001678 InsertAtEnd->getInstList().push_back(Res);
1679 return Res;
1680}
1681
Dan Gohman5476cfd2009-08-12 16:23:25 +00001682BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001683 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001684 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001685 return new BinaryOperator(Instruction::Sub,
1686 zero, Op,
1687 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001688}
1689
Dan Gohman5476cfd2009-08-12 16:23:25 +00001690BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001691 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001692 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001693 return new BinaryOperator(Instruction::Sub,
1694 zero, Op,
1695 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001696}
1697
Dan Gohman4ab44202009-12-18 02:58:50 +00001698BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1699 Instruction *InsertBefore) {
1700 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1701 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1702}
1703
1704BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1705 BasicBlock *InsertAtEnd) {
1706 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1707 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1708}
1709
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001710BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1711 Instruction *InsertBefore) {
1712 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1713 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1714}
1715
1716BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1717 BasicBlock *InsertAtEnd) {
1718 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1719 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1720}
1721
Dan Gohman5476cfd2009-08-12 16:23:25 +00001722BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001723 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001724 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001725 return new BinaryOperator(Instruction::FSub,
1726 zero, Op,
1727 Op->getType(), Name, InsertBefore);
1728}
1729
Dan Gohman5476cfd2009-08-12 16:23:25 +00001730BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001731 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001732 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001733 return new BinaryOperator(Instruction::FSub,
1734 zero, Op,
1735 Op->getType(), Name, InsertAtEnd);
1736}
1737
Dan Gohman5476cfd2009-08-12 16:23:25 +00001738BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001739 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001740 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001741 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001742 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001743 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001744 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001745 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001746 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001747 }
1748
1749 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001750 Op->getType(), Name, InsertBefore);
1751}
1752
Dan Gohman5476cfd2009-08-12 16:23:25 +00001753BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001754 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001755 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001756 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001757 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001758 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001759 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001760 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001761 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001762 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001763 }
1764
1765 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001766 Op->getType(), Name, InsertAtEnd);
1767}
1768
1769
1770// isConstantAllOnes - Helper function for several functions below
1771static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001772 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1773 return CI->isAllOnesValue();
1774 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1775 return CV->isAllOnesValue();
1776 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001777}
1778
Owen Andersonbb2501b2009-07-13 22:18:28 +00001779bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001780 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1781 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001782 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1783 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001784 return false;
1785}
1786
Owen Andersonbb2501b2009-07-13 22:18:28 +00001787bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001788 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1789 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001790 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001791 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001792 return false;
1793}
1794
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001795bool BinaryOperator::isNot(const Value *V) {
1796 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1797 return (Bop->getOpcode() == Instruction::Xor &&
1798 (isConstantAllOnes(Bop->getOperand(1)) ||
1799 isConstantAllOnes(Bop->getOperand(0))));
1800 return false;
1801}
1802
Chris Lattner2c7d1772005-04-24 07:28:37 +00001803Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001804 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001805}
1806
Chris Lattner2c7d1772005-04-24 07:28:37 +00001807const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1808 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001809}
1810
Dan Gohmana5b96452009-06-04 22:49:04 +00001811Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001812 return cast<BinaryOperator>(BinOp)->getOperand(1);
1813}
1814
1815const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1816 return getFNegArgument(const_cast<Value*>(BinOp));
1817}
1818
Chris Lattner2c7d1772005-04-24 07:28:37 +00001819Value *BinaryOperator::getNotArgument(Value *BinOp) {
1820 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1821 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1822 Value *Op0 = BO->getOperand(0);
1823 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001824 if (isConstantAllOnes(Op0)) return Op1;
1825
1826 assert(isConstantAllOnes(Op1));
1827 return Op0;
1828}
1829
Chris Lattner2c7d1772005-04-24 07:28:37 +00001830const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1831 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001832}
1833
1834
1835// swapOperands - Exchange the two operands to this instruction. This
1836// instruction is safe to use on any binary instruction and does not
1837// modify the semantics of the instruction. If the instruction is
1838// order dependent (SetLT f.e.) the opcode is changed.
1839//
1840bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001841 if (!isCommutative())
1842 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001843 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001844 return false;
1845}
1846
Dan Gohman1b849082009-09-07 23:54:19 +00001847void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1848 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1849}
1850
1851void BinaryOperator::setHasNoSignedWrap(bool b) {
1852 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1853}
1854
1855void BinaryOperator::setIsExact(bool b) {
1856 cast<SDivOperator>(this)->setIsExact(b);
1857}
1858
Nick Lewycky28a5f252009-09-27 21:33:04 +00001859bool BinaryOperator::hasNoUnsignedWrap() const {
1860 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1861}
1862
1863bool BinaryOperator::hasNoSignedWrap() const {
1864 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1865}
1866
1867bool BinaryOperator::isExact() const {
1868 return cast<SDivOperator>(this)->isExact();
1869}
1870
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001871//===----------------------------------------------------------------------===//
1872// CastInst Class
1873//===----------------------------------------------------------------------===//
1874
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001875// Just determine if this cast only deals with integral->integral conversion.
1876bool CastInst::isIntegerCast() const {
1877 switch (getOpcode()) {
1878 default: return false;
1879 case Instruction::ZExt:
1880 case Instruction::SExt:
1881 case Instruction::Trunc:
1882 return true;
1883 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00001884 return getOperand(0)->getType()->isIntegerTy() &&
1885 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001886 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001887}
1888
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001889bool CastInst::isLosslessCast() const {
1890 // Only BitCast can be lossless, exit fast if we're not BitCast
1891 if (getOpcode() != Instruction::BitCast)
1892 return false;
1893
1894 // Identity cast is always lossless
1895 const Type* SrcTy = getOperand(0)->getType();
1896 const Type* DstTy = getType();
1897 if (SrcTy == DstTy)
1898 return true;
1899
Reid Spencer8d9336d2006-12-31 05:26:44 +00001900 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00001901 if (SrcTy->isPointerTy())
1902 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001903 return false; // Other types have no identity values
1904}
1905
1906/// This function determines if the CastInst does not require any bits to be
1907/// changed in order to effect the cast. Essentially, it identifies cases where
1908/// no code gen is necessary for the cast, hence the name no-op cast. For
1909/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001910/// # bitcast i32* %x to i8*
1911/// # bitcast <2 x i32> %x to <4 x i16>
1912/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001913/// @brief Determine if the described cast is a no-op.
1914bool CastInst::isNoopCast(Instruction::CastOps Opcode,
1915 const Type *SrcTy,
1916 const Type *DestTy,
1917 const Type *IntPtrTy) {
1918 switch (Opcode) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001919 default:
1920 assert(!"Invalid CastOp");
1921 case Instruction::Trunc:
1922 case Instruction::ZExt:
1923 case Instruction::SExt:
1924 case Instruction::FPTrunc:
1925 case Instruction::FPExt:
1926 case Instruction::UIToFP:
1927 case Instruction::SIToFP:
1928 case Instruction::FPToUI:
1929 case Instruction::FPToSI:
1930 return false; // These always modify bits
1931 case Instruction::BitCast:
1932 return true; // BitCast never modifies bits.
1933 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001934 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001935 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001936 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001937 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001938 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001939 }
1940}
1941
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001942/// @brief Determine if a cast is a no-op.
1943bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1944 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
1945}
1946
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001947/// This function determines if a pair of casts can be eliminated and what
1948/// opcode should be used in the elimination. This assumes that there are two
1949/// instructions like this:
1950/// * %F = firstOpcode SrcTy %x to MidTy
1951/// * %S = secondOpcode MidTy %F to DstTy
1952/// The function returns a resultOpcode so these two casts can be replaced with:
1953/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1954/// If no such cast is permited, the function returns 0.
1955unsigned CastInst::isEliminableCastPair(
1956 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1957 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1958{
1959 // Define the 144 possibilities for these two cast instructions. The values
1960 // in this matrix determine what to do in a given situation and select the
1961 // case in the switch below. The rows correspond to firstOp, the columns
1962 // correspond to secondOp. In looking at the table below, keep in mind
1963 // the following cast properties:
1964 //
1965 // Size Compare Source Destination
1966 // Operator Src ? Size Type Sign Type Sign
1967 // -------- ------------ ------------------- ---------------------
1968 // TRUNC > Integer Any Integral Any
1969 // ZEXT < Integral Unsigned Integer Any
1970 // SEXT < Integral Signed Integer Any
1971 // FPTOUI n/a FloatPt n/a Integral Unsigned
1972 // FPTOSI n/a FloatPt n/a Integral Signed
1973 // UITOFP n/a Integral Unsigned FloatPt n/a
1974 // SITOFP n/a Integral Signed FloatPt n/a
1975 // FPTRUNC > FloatPt n/a FloatPt n/a
1976 // FPEXT < FloatPt n/a FloatPt n/a
1977 // PTRTOINT n/a Pointer n/a Integral Unsigned
1978 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmaneb7111b2010-04-07 23:22:42 +00001979 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001980 //
1981 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00001982 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1983 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00001984 // of the produced value (we no longer know the top-part is all zeros).
1985 // Further this conversion is often much more expensive for typical hardware,
1986 // and causes issues when building libgcc. We disallow fptosi+sext for the
1987 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001988 const unsigned numCastOps =
1989 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1990 static const uint8_t CastResults[numCastOps][numCastOps] = {
1991 // T F F U S F F P I B -+
1992 // R Z S P P I I T P 2 N T |
1993 // U E E 2 2 2 2 R E I T C +- secondOp
1994 // N X X U S F F N X N 2 V |
1995 // C T T I I P P C T T P T -+
1996 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1997 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1998 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001999 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2000 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002001 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2002 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2003 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2004 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2005 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2006 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2007 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2008 };
2009
2010 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2011 [secondOp-Instruction::CastOpsBegin];
2012 switch (ElimCase) {
2013 case 0:
2014 // categorically disallowed
2015 return 0;
2016 case 1:
2017 // allowed, use first cast's opcode
2018 return firstOp;
2019 case 2:
2020 // allowed, use second cast's opcode
2021 return secondOp;
2022 case 3:
2023 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002024 // is integer and we are not converting between a vector and a
Chris Lattner531732b2010-01-23 04:42:42 +00002025 // non vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002026 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002027 return firstOp;
2028 return 0;
2029 case 4:
2030 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002031 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002032 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002033 return firstOp;
2034 return 0;
2035 case 5:
2036 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002037 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002038 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002039 return secondOp;
2040 return 0;
2041 case 6:
2042 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002043 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002044 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002045 return secondOp;
2046 return 0;
2047 case 7: {
2048 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002049 if (!IntPtrTy)
2050 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002051 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2052 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002053 if (MidSize >= PtrSize)
2054 return Instruction::BitCast;
2055 return 0;
2056 }
2057 case 8: {
2058 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2059 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2060 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002061 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2062 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002063 if (SrcSize == DstSize)
2064 return Instruction::BitCast;
2065 else if (SrcSize < DstSize)
2066 return firstOp;
2067 return secondOp;
2068 }
2069 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2070 return Instruction::ZExt;
2071 case 10:
2072 // fpext followed by ftrunc is allowed if the bit size returned to is
2073 // the same as the original, in which case its just a bitcast
2074 if (SrcTy == DstTy)
2075 return Instruction::BitCast;
2076 return 0; // If the types are not the same we can't eliminate it.
2077 case 11:
2078 // bitcast followed by ptrtoint is allowed as long as the bitcast
2079 // is a pointer to pointer cast.
Duncan Sands19d0b472010-02-16 11:11:14 +00002080 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002081 return secondOp;
2082 return 0;
2083 case 12:
2084 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands19d0b472010-02-16 11:11:14 +00002085 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002086 return firstOp;
2087 return 0;
2088 case 13: {
2089 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002090 if (!IntPtrTy)
2091 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002092 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2093 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2094 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002095 if (SrcSize <= PtrSize && SrcSize == DstSize)
2096 return Instruction::BitCast;
2097 return 0;
2098 }
2099 case 99:
2100 // cast combination can't happen (error in input). This is for all cases
2101 // where the MidTy is not the same for the two cast instructions.
2102 assert(!"Invalid Cast Combination");
2103 return 0;
2104 default:
2105 assert(!"Error in CastResults table!!!");
2106 return 0;
2107 }
2108 return 0;
2109}
2110
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002111CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002112 const Twine &Name, Instruction *InsertBefore) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002113 // Construct and return the appropriate CastInst subclass
2114 switch (op) {
2115 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2116 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2117 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2118 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2119 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2120 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2121 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2122 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2123 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2124 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2125 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2126 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2127 default:
2128 assert(!"Invalid opcode provided");
2129 }
2130 return 0;
2131}
2132
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002133CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002134 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002135 // Construct and return the appropriate CastInst subclass
2136 switch (op) {
2137 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2138 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2139 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2140 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2141 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2142 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2143 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2144 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2145 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2146 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2147 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2148 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2149 default:
2150 assert(!"Invalid opcode provided");
2151 }
2152 return 0;
2153}
2154
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002155CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002156 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002157 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002158 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002159 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2160 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002161}
2162
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002163CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002164 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002165 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002166 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002167 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2168 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002169}
2170
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002171CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002172 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002173 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002174 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002175 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2176 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002177}
2178
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002179CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002180 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002181 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002182 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002183 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2184 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002185}
2186
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002187CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002188 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002189 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002190 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002191 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2192 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002193}
2194
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002195CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002196 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002197 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002198 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002199 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2200 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002201}
2202
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002203CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002204 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002205 BasicBlock *InsertAtEnd) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002206 assert(S->getType()->isPointerTy() && "Invalid cast");
2207 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002208 "Invalid cast");
2209
Duncan Sands9dff9be2010-02-15 16:12:20 +00002210 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002211 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2212 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002213}
2214
2215/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002216CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002217 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002218 Instruction *InsertBefore) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002219 assert(S->getType()->isPointerTy() && "Invalid cast");
2220 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002221 "Invalid cast");
2222
Duncan Sands9dff9be2010-02-15 16:12:20 +00002223 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002224 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2225 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002226}
2227
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002228CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002229 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002230 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002231 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002232 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002233 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2234 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002235 Instruction::CastOps opcode =
2236 (SrcBits == DstBits ? Instruction::BitCast :
2237 (SrcBits > DstBits ? Instruction::Trunc :
2238 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002239 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002240}
2241
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002242CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002243 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002244 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002245 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002246 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002247 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2248 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002249 Instruction::CastOps opcode =
2250 (SrcBits == DstBits ? Instruction::BitCast :
2251 (SrcBits > DstBits ? Instruction::Trunc :
2252 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002253 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002254}
2255
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002256CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002257 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002258 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002259 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002260 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002261 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2262 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002263 Instruction::CastOps opcode =
2264 (SrcBits == DstBits ? Instruction::BitCast :
2265 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002266 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002267}
2268
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002269CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002270 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002271 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002272 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002273 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002274 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2275 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002276 Instruction::CastOps opcode =
2277 (SrcBits == DstBits ? Instruction::BitCast :
2278 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002279 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002280}
2281
Duncan Sands55e50902008-01-06 10:12:28 +00002282// Check whether it is valid to call getCastOpcode for these types.
2283// This routine must be kept in sync with getCastOpcode.
2284bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2285 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2286 return false;
2287
2288 if (SrcTy == DestTy)
2289 return true;
2290
2291 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002292 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2293 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002294
2295 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002296 if (DestTy->isIntegerTy()) { // Casting to integral
2297 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002298 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002299 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002300 return true;
2301 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002302 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002303 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002304 } else { // Casting from something else
Duncan Sands19d0b472010-02-16 11:11:14 +00002305 return SrcTy->isPointerTy();
Duncan Sands55e50902008-01-06 10:12:28 +00002306 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002307 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2308 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002309 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002310 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002311 return true;
2312 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002313 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002314 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002315 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002316 return false;
2317 }
2318 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002319 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002320 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002321 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002322 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002323 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002324 return DestPTy->getBitWidth() == SrcBits;
2325 }
Duncan Sands19d0b472010-02-16 11:11:14 +00002326 } else if (DestTy->isPointerTy()) { // Casting to pointer
2327 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002328 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002329 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002330 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002331 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002332 return false;
2333 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002334 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002335 return false;
2336 }
2337}
2338
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002339// Provide a way to get a "cast" where the cast opcode is inferred from the
2340// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002341// logic in the castIsValid function below. This axiom should hold:
2342// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2343// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002344// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002345// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002346Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002347CastInst::getCastOpcode(
2348 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002349 // Get the bit sizes, we'll need these
2350 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002351 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2352 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002353
Duncan Sands55e50902008-01-06 10:12:28 +00002354 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2355 "Only first class types are castable!");
2356
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002357 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002358 if (DestTy->isIntegerTy()) { // Casting to integral
2359 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002360 if (DestBits < SrcBits)
2361 return Trunc; // int -> smaller int
2362 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002363 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002364 return SExt; // signed -> SEXT
2365 else
2366 return ZExt; // unsigned -> ZEXT
2367 } else {
2368 return BitCast; // Same size, No-op cast
2369 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002370 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002371 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002372 return FPToSI; // FP -> sint
2373 else
2374 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002375 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002376 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002377 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002378 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002379 return BitCast; // Same size, no-op cast
2380 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002381 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002382 "Casting from a value that is not first-class type");
2383 return PtrToInt; // ptr -> int
2384 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002385 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2386 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002387 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002388 return SIToFP; // sint -> FP
2389 else
2390 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002391 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002392 if (DestBits < SrcBits) {
2393 return FPTrunc; // FP -> smaller FP
2394 } else if (DestBits > SrcBits) {
2395 return FPExt; // FP -> larger FP
2396 } else {
2397 return BitCast; // same size, no-op cast
2398 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002399 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002400 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002401 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002402 PTy = NULL;
2403 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002404 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002405 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002406 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002407 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2408 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002409 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002410 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002411 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002412 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002413 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002414 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002415 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002416 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002417 }
Duncan Sands19d0b472010-02-16 11:11:14 +00002418 } else if (DestTy->isPointerTy()) {
2419 if (SrcTy->isPointerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002420 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002421 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002422 return IntToPtr; // int -> ptr
2423 } else {
2424 assert(!"Casting pointer to other than pointer or int");
2425 }
2426 } else {
2427 assert(!"Casting to type that is not first-class");
2428 }
2429
2430 // If we fall through to here we probably hit an assertion cast above
2431 // and assertions are not turned on. Anything we return is an error, so
2432 // BitCast is as good a choice as any.
2433 return BitCast;
2434}
2435
2436//===----------------------------------------------------------------------===//
2437// CastInst SubClass Constructors
2438//===----------------------------------------------------------------------===//
2439
2440/// Check that the construction parameters for a CastInst are correct. This
2441/// could be broken out into the separate constructors but it is useful to have
2442/// it in one place and to eliminate the redundant code for getting the sizes
2443/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002444bool
2445CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002446
2447 // Check for type sanity on the arguments
2448 const Type *SrcTy = S->getType();
Chris Lattner37bc78a2010-01-26 21:51:43 +00002449 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2450 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002451 return false;
2452
2453 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002454 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2455 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002456
2457 // Switch on the opcode provided
2458 switch (op) {
2459 default: return false; // This is an input error
2460 case Instruction::Trunc:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002461 return SrcTy->isIntOrIntVectorTy() &&
2462 DstTy->isIntOrIntVectorTy()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002463 case Instruction::ZExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002464 return SrcTy->isIntOrIntVectorTy() &&
2465 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002466 case Instruction::SExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002467 return SrcTy->isIntOrIntVectorTy() &&
2468 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002469 case Instruction::FPTrunc:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002470 return SrcTy->isFPOrFPVectorTy() &&
2471 DstTy->isFPOrFPVectorTy() &&
Dan Gohman550c9af2008-08-14 20:04:46 +00002472 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002473 case Instruction::FPExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002474 return SrcTy->isFPOrFPVectorTy() &&
2475 DstTy->isFPOrFPVectorTy() &&
Dan Gohman550c9af2008-08-14 20:04:46 +00002476 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002477 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002478 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002479 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2480 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002481 return SVTy->getElementType()->isIntOrIntVectorTy() &&
2482 DVTy->getElementType()->isFPOrFPVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002483 SVTy->getNumElements() == DVTy->getNumElements();
2484 }
2485 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002486 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002487 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002488 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002489 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2490 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002491 return SVTy->getElementType()->isFPOrFPVectorTy() &&
2492 DVTy->getElementType()->isIntOrIntVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002493 SVTy->getNumElements() == DVTy->getNumElements();
2494 }
2495 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002496 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002497 case Instruction::PtrToInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00002498 return SrcTy->isPointerTy() && DstTy->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002499 case Instruction::IntToPtr:
Duncan Sands19d0b472010-02-16 11:11:14 +00002500 return SrcTy->isIntegerTy() && DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002501 case Instruction::BitCast:
2502 // BitCast implies a no-op cast of type only. No bits change.
2503 // However, you can't cast pointers to anything but pointers.
Duncan Sands19d0b472010-02-16 11:11:14 +00002504 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002505 return false;
2506
Duncan Sands55e50902008-01-06 10:12:28 +00002507 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002508 // these cases, the cast is okay if the source and destination bit widths
2509 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002510 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002511 }
2512}
2513
2514TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002515 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002516) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002517 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002518}
2519
2520TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002521 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002522) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002523 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002524}
2525
2526ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002527 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002528) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002529 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002530}
2531
2532ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002533 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002534) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002535 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002536}
2537SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002538 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002539) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002540 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002541}
2542
Jeff Cohencc08c832006-12-02 02:22:01 +00002543SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002544 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002545) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002546 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002547}
2548
2549FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002550 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002551) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002552 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002553}
2554
2555FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002556 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002557) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002558 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002559}
2560
2561FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002562 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002563) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002564 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002565}
2566
2567FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002568 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002569) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002570 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002571}
2572
2573UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002574 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002575) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002576 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002577}
2578
2579UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002580 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002581) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002582 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002583}
2584
2585SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002586 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002587) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002588 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002589}
2590
2591SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002592 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002593) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002594 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002595}
2596
2597FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002598 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002599) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002600 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002601}
2602
2603FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002604 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002605) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002606 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002607}
2608
2609FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002610 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002611) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002612 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002613}
2614
2615FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002616 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002617) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002618 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002619}
2620
2621PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002622 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002623) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002624 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002625}
2626
2627PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002628 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002629) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002630 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002631}
2632
2633IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002634 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002635) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002636 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002637}
2638
2639IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002640 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002641) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002642 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002643}
2644
2645BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002646 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002647) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002648 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002649}
2650
2651BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002652 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002653) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002654 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002655}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002656
2657//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002658// CmpInst Classes
2659//===----------------------------------------------------------------------===//
2660
Chris Lattneraec33da2010-01-22 06:25:37 +00002661void CmpInst::Anchor() const {}
2662
Nate Begemand2195702008-05-12 19:01:56 +00002663CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002664 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002665 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002666 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002667 OperandTraits<CmpInst>::op_begin(this),
2668 OperandTraits<CmpInst>::operands(this),
2669 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002670 Op<0>() = LHS;
2671 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002672 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002673 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002674}
Gabor Greiff6caff662008-05-10 08:32:32 +00002675
Nate Begemand2195702008-05-12 19:01:56 +00002676CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002677 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002678 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002679 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002680 OperandTraits<CmpInst>::op_begin(this),
2681 OperandTraits<CmpInst>::operands(this),
2682 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002683 Op<0>() = LHS;
2684 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002685 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002686 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002687}
2688
2689CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002690CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002691 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002692 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002693 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002694 if (InsertBefore)
2695 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2696 S1, S2, Name);
2697 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002698 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002699 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002700 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002701
2702 if (InsertBefore)
2703 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2704 S1, S2, Name);
2705 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002706 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002707 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002708}
2709
2710CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002711CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002712 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002713 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002714 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2715 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002716 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002717 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2718 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002719}
2720
2721void CmpInst::swapOperands() {
2722 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2723 IC->swapOperands();
2724 else
2725 cast<FCmpInst>(this)->swapOperands();
2726}
2727
2728bool CmpInst::isCommutative() {
2729 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2730 return IC->isCommutative();
2731 return cast<FCmpInst>(this)->isCommutative();
2732}
2733
2734bool CmpInst::isEquality() {
2735 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2736 return IC->isEquality();
2737 return cast<FCmpInst>(this)->isEquality();
2738}
2739
2740
Dan Gohman4e724382008-05-31 02:47:54 +00002741CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002742 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002743 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002744 case ICMP_EQ: return ICMP_NE;
2745 case ICMP_NE: return ICMP_EQ;
2746 case ICMP_UGT: return ICMP_ULE;
2747 case ICMP_ULT: return ICMP_UGE;
2748 case ICMP_UGE: return ICMP_ULT;
2749 case ICMP_ULE: return ICMP_UGT;
2750 case ICMP_SGT: return ICMP_SLE;
2751 case ICMP_SLT: return ICMP_SGE;
2752 case ICMP_SGE: return ICMP_SLT;
2753 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002754
Dan Gohman4e724382008-05-31 02:47:54 +00002755 case FCMP_OEQ: return FCMP_UNE;
2756 case FCMP_ONE: return FCMP_UEQ;
2757 case FCMP_OGT: return FCMP_ULE;
2758 case FCMP_OLT: return FCMP_UGE;
2759 case FCMP_OGE: return FCMP_ULT;
2760 case FCMP_OLE: return FCMP_UGT;
2761 case FCMP_UEQ: return FCMP_ONE;
2762 case FCMP_UNE: return FCMP_OEQ;
2763 case FCMP_UGT: return FCMP_OLE;
2764 case FCMP_ULT: return FCMP_OGE;
2765 case FCMP_UGE: return FCMP_OLT;
2766 case FCMP_ULE: return FCMP_OGT;
2767 case FCMP_ORD: return FCMP_UNO;
2768 case FCMP_UNO: return FCMP_ORD;
2769 case FCMP_TRUE: return FCMP_FALSE;
2770 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002771 }
2772}
2773
Reid Spencer266e42b2006-12-23 06:05:41 +00002774ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2775 switch (pred) {
2776 default: assert(! "Unknown icmp predicate!");
2777 case ICMP_EQ: case ICMP_NE:
2778 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2779 return pred;
2780 case ICMP_UGT: return ICMP_SGT;
2781 case ICMP_ULT: return ICMP_SLT;
2782 case ICMP_UGE: return ICMP_SGE;
2783 case ICMP_ULE: return ICMP_SLE;
2784 }
2785}
2786
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002787ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2788 switch (pred) {
2789 default: assert(! "Unknown icmp predicate!");
2790 case ICMP_EQ: case ICMP_NE:
2791 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2792 return pred;
2793 case ICMP_SGT: return ICMP_UGT;
2794 case ICMP_SLT: return ICMP_ULT;
2795 case ICMP_SGE: return ICMP_UGE;
2796 case ICMP_SLE: return ICMP_ULE;
2797 }
2798}
2799
Reid Spencer0286bc12007-02-28 22:00:54 +00002800/// Initialize a set of values that all satisfy the condition with C.
2801///
2802ConstantRange
2803ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2804 APInt Lower(C);
2805 APInt Upper(C);
2806 uint32_t BitWidth = C.getBitWidth();
2807 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002808 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002809 case ICmpInst::ICMP_EQ: Upper++; break;
2810 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00002811 case ICmpInst::ICMP_ULT:
2812 Lower = APInt::getMinValue(BitWidth);
2813 // Check for an empty-set condition.
2814 if (Lower == Upper)
2815 return ConstantRange(BitWidth, /*isFullSet=*/false);
2816 break;
2817 case ICmpInst::ICMP_SLT:
2818 Lower = APInt::getSignedMinValue(BitWidth);
2819 // Check for an empty-set condition.
2820 if (Lower == Upper)
2821 return ConstantRange(BitWidth, /*isFullSet=*/false);
2822 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00002823 case ICmpInst::ICMP_UGT:
2824 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002825 // Check for an empty-set condition.
2826 if (Lower == Upper)
2827 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002828 break;
2829 case ICmpInst::ICMP_SGT:
2830 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002831 // Check for an empty-set condition.
2832 if (Lower == Upper)
2833 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002834 break;
2835 case ICmpInst::ICMP_ULE:
2836 Lower = APInt::getMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002837 // Check for a full-set condition.
2838 if (Lower == Upper)
2839 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002840 break;
2841 case ICmpInst::ICMP_SLE:
2842 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002843 // Check for a full-set condition.
2844 if (Lower == Upper)
2845 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002846 break;
2847 case ICmpInst::ICMP_UGE:
2848 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002849 // Check for a full-set condition.
2850 if (Lower == Upper)
2851 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002852 break;
2853 case ICmpInst::ICMP_SGE:
2854 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002855 // Check for a full-set condition.
2856 if (Lower == Upper)
2857 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002858 break;
2859 }
2860 return ConstantRange(Lower, Upper);
2861}
2862
Dan Gohman4e724382008-05-31 02:47:54 +00002863CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002864 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002865 default: assert(!"Unknown cmp predicate!");
2866 case ICMP_EQ: case ICMP_NE:
2867 return pred;
2868 case ICMP_SGT: return ICMP_SLT;
2869 case ICMP_SLT: return ICMP_SGT;
2870 case ICMP_SGE: return ICMP_SLE;
2871 case ICMP_SLE: return ICMP_SGE;
2872 case ICMP_UGT: return ICMP_ULT;
2873 case ICMP_ULT: return ICMP_UGT;
2874 case ICMP_UGE: return ICMP_ULE;
2875 case ICMP_ULE: return ICMP_UGE;
2876
Reid Spencerd9436b62006-11-20 01:22:35 +00002877 case FCMP_FALSE: case FCMP_TRUE:
2878 case FCMP_OEQ: case FCMP_ONE:
2879 case FCMP_UEQ: case FCMP_UNE:
2880 case FCMP_ORD: case FCMP_UNO:
2881 return pred;
2882 case FCMP_OGT: return FCMP_OLT;
2883 case FCMP_OLT: return FCMP_OGT;
2884 case FCMP_OGE: return FCMP_OLE;
2885 case FCMP_OLE: return FCMP_OGE;
2886 case FCMP_UGT: return FCMP_ULT;
2887 case FCMP_ULT: return FCMP_UGT;
2888 case FCMP_UGE: return FCMP_ULE;
2889 case FCMP_ULE: return FCMP_UGE;
2890 }
2891}
2892
Reid Spencer266e42b2006-12-23 06:05:41 +00002893bool CmpInst::isUnsigned(unsigned short predicate) {
2894 switch (predicate) {
2895 default: return false;
2896 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2897 case ICmpInst::ICMP_UGE: return true;
2898 }
2899}
2900
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002901bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002902 switch (predicate) {
2903 default: return false;
2904 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2905 case ICmpInst::ICMP_SGE: return true;
2906 }
2907}
2908
2909bool CmpInst::isOrdered(unsigned short predicate) {
2910 switch (predicate) {
2911 default: return false;
2912 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2913 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2914 case FCmpInst::FCMP_ORD: return true;
2915 }
2916}
2917
2918bool CmpInst::isUnordered(unsigned short predicate) {
2919 switch (predicate) {
2920 default: return false;
2921 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2922 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2923 case FCmpInst::FCMP_UNO: return true;
2924 }
2925}
2926
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002927bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
2928 switch(predicate) {
2929 default: return false;
2930 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
2931 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
2932 }
2933}
2934
2935bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
2936 switch(predicate) {
2937 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
2938 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
2939 default: return false;
2940 }
2941}
2942
2943
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002944//===----------------------------------------------------------------------===//
2945// SwitchInst Implementation
2946//===----------------------------------------------------------------------===//
2947
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002948void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002949 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002950 ReservedSpace = 2+NumCases*2;
2951 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002952 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002953
Gabor Greif2d3024d2008-05-26 21:33:52 +00002954 OperandList[0] = Value;
2955 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002956}
2957
Chris Lattner2195fc42007-02-24 00:55:48 +00002958/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2959/// switch on and a default destination. The number of additional cases can
2960/// be specified here to make memory allocation more efficient. This
2961/// constructor can also autoinsert before another instruction.
2962SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2963 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00002964 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2965 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002966 init(Value, Default, NumCases);
2967}
2968
2969/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2970/// switch on and a default destination. The number of additional cases can
2971/// be specified here to make memory allocation more efficient. This
2972/// constructor also autoinserts at the end of the specified BasicBlock.
2973SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2974 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00002975 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2976 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002977 init(Value, Default, NumCases);
2978}
2979
Misha Brukmanb1c93172005-04-21 23:48:37 +00002980SwitchInst::SwitchInst(const SwitchInst &SI)
Owen Anderson55f1c092009-08-13 21:58:54 +00002981 : TerminatorInst(Type::getVoidTy(SI.getContext()), Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002982 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002983 Use *OL = OperandList, *InOL = SI.OperandList;
2984 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002985 OL[i] = InOL[i];
2986 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002987 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002988 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002989}
2990
Gordon Henriksen14a55692007-12-10 02:14:30 +00002991SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002992 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002993}
2994
2995
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002996/// addCase - Add an entry to the switch instruction...
2997///
Chris Lattner47ac1872005-02-24 05:32:09 +00002998void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002999 unsigned OpNo = NumOperands;
3000 if (OpNo+2 > ReservedSpace)
3001 resizeOperands(0); // Get more space!
3002 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003003 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003004 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00003005 OperandList[OpNo] = OnVal;
3006 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003007}
3008
3009/// removeCase - This method removes the specified successor from the switch
3010/// instruction. Note that this cannot be used to remove the default
3011/// destination (successor #0).
3012///
3013void SwitchInst::removeCase(unsigned idx) {
3014 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003015 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
3016
3017 unsigned NumOps = getNumOperands();
3018 Use *OL = OperandList;
3019
3020 // Move everything after this operand down.
3021 //
3022 // FIXME: we could just swap with the end of the list, then erase. However,
3023 // client might not expect this to happen. The code as it is thrashes the
3024 // use/def lists, which is kinda lame.
3025 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
3026 OL[i-2] = OL[i];
3027 OL[i-2+1] = OL[i+1];
3028 }
3029
3030 // Nuke the last value.
3031 OL[NumOps-2].set(0);
3032 OL[NumOps-2+1].set(0);
3033 NumOperands = NumOps-2;
3034}
3035
3036/// resizeOperands - resize operands - This adjusts the length of the operands
3037/// list according to the following behavior:
3038/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00003039/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003040/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3041/// 3. If NumOps == NumOperands, trim the reserved space.
3042///
3043void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003044 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003045 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003046 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003047 } else if (NumOps*2 > NumOperands) {
3048 // No resize needed.
3049 if (ReservedSpace >= NumOps) return;
3050 } else if (NumOps == NumOperands) {
3051 if (ReservedSpace == NumOps) return;
3052 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003053 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003054 }
3055
3056 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003057 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003058 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003059 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003060 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003061 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003062 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003063 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003064}
3065
3066
3067BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3068 return getSuccessor(idx);
3069}
3070unsigned SwitchInst::getNumSuccessorsV() const {
3071 return getNumSuccessors();
3072}
3073void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3074 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003075}
Chris Lattnerf22be932004-10-15 23:52:53 +00003076
Chris Lattner3ed871f2009-10-27 19:13:16 +00003077//===----------------------------------------------------------------------===//
3078// SwitchInst Implementation
3079//===----------------------------------------------------------------------===//
3080
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003081void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003082 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003083 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003084 ReservedSpace = 1+NumDests;
3085 NumOperands = 1;
3086 OperandList = allocHungoffUses(ReservedSpace);
3087
3088 OperandList[0] = Address;
3089}
3090
3091
3092/// resizeOperands - resize operands - This adjusts the length of the operands
3093/// list according to the following behavior:
3094/// 1. If NumOps == 0, grow the operand list in response to a push_back style
3095/// of operation. This grows the number of ops by 2 times.
3096/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3097/// 3. If NumOps == NumOperands, trim the reserved space.
3098///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003099void IndirectBrInst::resizeOperands(unsigned NumOps) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003100 unsigned e = getNumOperands();
3101 if (NumOps == 0) {
3102 NumOps = e*2;
3103 } else if (NumOps*2 > NumOperands) {
3104 // No resize needed.
3105 if (ReservedSpace >= NumOps) return;
3106 } else if (NumOps == NumOperands) {
3107 if (ReservedSpace == NumOps) return;
3108 } else {
3109 return;
3110 }
3111
3112 ReservedSpace = NumOps;
3113 Use *NewOps = allocHungoffUses(NumOps);
3114 Use *OldOps = OperandList;
3115 for (unsigned i = 0; i != e; ++i)
3116 NewOps[i] = OldOps[i];
3117 OperandList = NewOps;
3118 if (OldOps) Use::zap(OldOps, OldOps + e, true);
3119}
3120
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003121IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3122 Instruction *InsertBefore)
3123: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003124 0, 0, InsertBefore) {
3125 init(Address, NumCases);
3126}
3127
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003128IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3129 BasicBlock *InsertAtEnd)
3130: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003131 0, 0, InsertAtEnd) {
3132 init(Address, NumCases);
3133}
3134
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003135IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3136 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003137 allocHungoffUses(IBI.getNumOperands()),
3138 IBI.getNumOperands()) {
3139 Use *OL = OperandList, *InOL = IBI.OperandList;
3140 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3141 OL[i] = InOL[i];
3142 SubclassOptionalData = IBI.SubclassOptionalData;
3143}
3144
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003145IndirectBrInst::~IndirectBrInst() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003146 dropHungoffUses(OperandList);
3147}
3148
3149/// addDestination - Add a destination.
3150///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003151void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003152 unsigned OpNo = NumOperands;
3153 if (OpNo+1 > ReservedSpace)
3154 resizeOperands(0); // Get more space!
3155 // Initialize some new operands.
3156 assert(OpNo < ReservedSpace && "Growing didn't work!");
3157 NumOperands = OpNo+1;
3158 OperandList[OpNo] = DestBB;
3159}
3160
3161/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003162/// indirectbr instruction.
3163void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003164 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3165
3166 unsigned NumOps = getNumOperands();
3167 Use *OL = OperandList;
3168
3169 // Replace this value with the last one.
3170 OL[idx+1] = OL[NumOps-1];
3171
3172 // Nuke the last value.
3173 OL[NumOps-1].set(0);
3174 NumOperands = NumOps-1;
3175}
3176
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003177BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003178 return getSuccessor(idx);
3179}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003180unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003181 return getNumSuccessors();
3182}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003183void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003184 setSuccessor(idx, B);
3185}
3186
3187//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003188// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003189//===----------------------------------------------------------------------===//
3190
Chris Lattnerf22be932004-10-15 23:52:53 +00003191// Define these methods here so vtables don't get emitted into every translation
3192// unit that uses these classes.
3193
Devang Patel11cf3f42009-10-27 22:16:29 +00003194GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3195 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003196}
3197
Devang Patel11cf3f42009-10-27 22:16:29 +00003198BinaryOperator *BinaryOperator::clone_impl() const {
3199 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003200}
3201
Devang Patel11cf3f42009-10-27 22:16:29 +00003202FCmpInst* FCmpInst::clone_impl() const {
3203 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003204}
3205
Devang Patel11cf3f42009-10-27 22:16:29 +00003206ICmpInst* ICmpInst::clone_impl() const {
3207 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003208}
3209
Devang Patel11cf3f42009-10-27 22:16:29 +00003210ExtractValueInst *ExtractValueInst::clone_impl() const {
3211 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003212}
3213
Devang Patel11cf3f42009-10-27 22:16:29 +00003214InsertValueInst *InsertValueInst::clone_impl() const {
3215 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003216}
3217
Devang Patel11cf3f42009-10-27 22:16:29 +00003218AllocaInst *AllocaInst::clone_impl() const {
3219 return new AllocaInst(getAllocatedType(),
3220 (Value*)getOperand(0),
3221 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003222}
3223
Devang Patel11cf3f42009-10-27 22:16:29 +00003224LoadInst *LoadInst::clone_impl() const {
3225 return new LoadInst(getOperand(0),
3226 Twine(), isVolatile(),
3227 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003228}
3229
Devang Patel11cf3f42009-10-27 22:16:29 +00003230StoreInst *StoreInst::clone_impl() const {
3231 return new StoreInst(getOperand(0), getOperand(1),
3232 isVolatile(), getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003233}
3234
Devang Patel11cf3f42009-10-27 22:16:29 +00003235TruncInst *TruncInst::clone_impl() const {
3236 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003237}
3238
Devang Patel11cf3f42009-10-27 22:16:29 +00003239ZExtInst *ZExtInst::clone_impl() const {
3240 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003241}
3242
Devang Patel11cf3f42009-10-27 22:16:29 +00003243SExtInst *SExtInst::clone_impl() const {
3244 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003245}
3246
Devang Patel11cf3f42009-10-27 22:16:29 +00003247FPTruncInst *FPTruncInst::clone_impl() const {
3248 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003249}
3250
Devang Patel11cf3f42009-10-27 22:16:29 +00003251FPExtInst *FPExtInst::clone_impl() const {
3252 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003253}
3254
Devang Patel11cf3f42009-10-27 22:16:29 +00003255UIToFPInst *UIToFPInst::clone_impl() const {
3256 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003257}
3258
Devang Patel11cf3f42009-10-27 22:16:29 +00003259SIToFPInst *SIToFPInst::clone_impl() const {
3260 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003261}
3262
Devang Patel11cf3f42009-10-27 22:16:29 +00003263FPToUIInst *FPToUIInst::clone_impl() const {
3264 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003265}
3266
Devang Patel11cf3f42009-10-27 22:16:29 +00003267FPToSIInst *FPToSIInst::clone_impl() const {
3268 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003269}
3270
Devang Patel11cf3f42009-10-27 22:16:29 +00003271PtrToIntInst *PtrToIntInst::clone_impl() const {
3272 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003273}
3274
Devang Patel11cf3f42009-10-27 22:16:29 +00003275IntToPtrInst *IntToPtrInst::clone_impl() const {
3276 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003277}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003278
Devang Patel11cf3f42009-10-27 22:16:29 +00003279BitCastInst *BitCastInst::clone_impl() const {
3280 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003281}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003282
Devang Patel11cf3f42009-10-27 22:16:29 +00003283CallInst *CallInst::clone_impl() const {
3284 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003285}
3286
Devang Patel11cf3f42009-10-27 22:16:29 +00003287SelectInst *SelectInst::clone_impl() const {
3288 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003289}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003290
Devang Patel11cf3f42009-10-27 22:16:29 +00003291VAArgInst *VAArgInst::clone_impl() const {
3292 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003293}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003294
Devang Patel11cf3f42009-10-27 22:16:29 +00003295ExtractElementInst *ExtractElementInst::clone_impl() const {
3296 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003297}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003298
Devang Patel11cf3f42009-10-27 22:16:29 +00003299InsertElementInst *InsertElementInst::clone_impl() const {
3300 return InsertElementInst::Create(getOperand(0),
3301 getOperand(1),
3302 getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003303}
3304
Devang Patel11cf3f42009-10-27 22:16:29 +00003305ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3306 return new ShuffleVectorInst(getOperand(0),
3307 getOperand(1),
3308 getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003309}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003310
Devang Patel11cf3f42009-10-27 22:16:29 +00003311PHINode *PHINode::clone_impl() const {
3312 return new PHINode(*this);
3313}
3314
3315ReturnInst *ReturnInst::clone_impl() const {
3316 return new(getNumOperands()) ReturnInst(*this);
3317}
3318
3319BranchInst *BranchInst::clone_impl() const {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00003320 unsigned Ops(getNumOperands());
Devang Patel11cf3f42009-10-27 22:16:29 +00003321 return new(Ops, Ops == 1) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003322}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003323
Devang Patel11cf3f42009-10-27 22:16:29 +00003324SwitchInst *SwitchInst::clone_impl() const {
3325 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003326}
3327
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003328IndirectBrInst *IndirectBrInst::clone_impl() const {
3329 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003330}
3331
3332
Devang Patel11cf3f42009-10-27 22:16:29 +00003333InvokeInst *InvokeInst::clone_impl() const {
3334 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003335}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003336
Devang Patel11cf3f42009-10-27 22:16:29 +00003337UnwindInst *UnwindInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003338 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003339 return new UnwindInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003340}
3341
Devang Patel11cf3f42009-10-27 22:16:29 +00003342UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003343 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003344 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003345}