blob: 57b7f3f3d649101d84d711f811ef1bff1e3d7930 [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 Greif6d673952010-07-16 09:38:02 +000036 ? cast</*FIXME: CallInst*/User>(II)->op_end() - 1 // Skip Callee
37 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000038}
39
Gordon Henriksen14a55692007-12-10 02:14:30 +000040//===----------------------------------------------------------------------===//
41// TerminatorInst Class
42//===----------------------------------------------------------------------===//
43
44// Out of line virtual method, so the vtable, etc has a home.
45TerminatorInst::~TerminatorInst() {
46}
47
Gabor Greiff6caff662008-05-10 08:32:32 +000048//===----------------------------------------------------------------------===//
49// UnaryInstruction Class
50//===----------------------------------------------------------------------===//
51
Gordon Henriksen14a55692007-12-10 02:14:30 +000052// Out of line virtual method, so the vtable, etc has a home.
53UnaryInstruction::~UnaryInstruction() {
54}
55
Chris Lattnerafdb3de2005-01-29 00:35:16 +000056//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000057// SelectInst Class
58//===----------------------------------------------------------------------===//
59
60/// areInvalidOperands - Return a string if the specified operands are invalid
61/// for a select operation, otherwise return null.
62const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
63 if (Op1->getType() != Op2->getType())
64 return "both values to select must have same type";
65
66 if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
67 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000068 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000069 return "vector select condition element type must be i1";
70 const VectorType *ET = dyn_cast<VectorType>(Op1->getType());
71 if (ET == 0)
72 return "selected values for vector select must be vectors";
73 if (ET->getNumElements() != VT->getNumElements())
74 return "vector select requires selected vectors to have "
75 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000076 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000077 return "select condition must be i1 or <n x i1>";
78 }
79 return 0;
80}
81
82
83//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000084// PHINode Class
85//===----------------------------------------------------------------------===//
86
87PHINode::PHINode(const PHINode &PN)
88 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +000089 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +000090 ReservedSpace(PN.getNumOperands()) {
91 Use *OL = OperandList;
92 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +000093 OL[i] = PN.getOperand(i);
94 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +000095 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +000096 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +000097}
98
Gordon Henriksen14a55692007-12-10 02:14:30 +000099PHINode::~PHINode() {
Chris Lattner7d6aac82008-06-16 04:02:40 +0000100 if (OperandList)
101 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000102}
103
104// removeIncomingValue - Remove an incoming value. This is useful if a
105// predecessor basic block is deleted.
106Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
107 unsigned NumOps = getNumOperands();
108 Use *OL = OperandList;
109 assert(Idx*2 < NumOps && "BB not in PHI node!");
110 Value *Removed = OL[Idx*2];
111
112 // Move everything after this operand down.
113 //
114 // FIXME: we could just swap with the end of the list, then erase. However,
115 // client might not expect this to happen. The code as it is thrashes the
116 // use/def lists, which is kinda lame.
117 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
118 OL[i-2] = OL[i];
119 OL[i-2+1] = OL[i+1];
120 }
121
122 // Nuke the last value.
123 OL[NumOps-2].set(0);
124 OL[NumOps-2+1].set(0);
125 NumOperands = NumOps-2;
126
127 // If the PHI node is dead, because it has zero entries, nuke it now.
128 if (NumOps == 2 && DeletePHIIfEmpty) {
129 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000130 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000131 eraseFromParent();
132 }
133 return Removed;
134}
135
136/// resizeOperands - resize operands - This adjusts the length of the operands
137/// list according to the following behavior:
138/// 1. If NumOps == 0, grow the operand list in response to a push_back style
139/// of operation. This grows the number of ops by 1.5 times.
140/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
141/// 3. If NumOps == NumOperands, trim the reserved space.
142///
143void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000144 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000145 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000146 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000147 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
148 } else if (NumOps*2 > NumOperands) {
149 // No resize needed.
150 if (ReservedSpace >= NumOps) return;
151 } else if (NumOps == NumOperands) {
152 if (ReservedSpace == NumOps) return;
153 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000154 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000155 }
156
157 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000158 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000159 Use *NewOps = allocHungoffUses(NumOps);
Dan Gohman031f0bb2008-06-23 16:45:24 +0000160 std::copy(OldOps, OldOps + e, NewOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000161 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000162 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000163}
164
Nate Begemanb3923212005-08-04 23:24:19 +0000165/// hasConstantValue - If the specified PHI node always merges together the same
166/// value, return the value, otherwise return null.
167///
Dan Gohman22571482009-09-03 15:34:35 +0000168/// If the PHI has undef operands, but all the rest of the operands are
169/// some unique value, return that value if it can be proved that the
170/// value dominates the PHI. If DT is null, use a conservative check,
171/// otherwise use DT to test for dominance.
172///
173Value *PHINode::hasConstantValue(DominatorTree *DT) const {
Chris Lattner7d08da62009-09-21 22:27:34 +0000174 // If the PHI node only has one incoming value, eliminate the PHI node.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000175 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000176 if (getIncomingValue(0) != this) // not X = phi X
177 return getIncomingValue(0);
Chris Lattner7d08da62009-09-21 22:27:34 +0000178 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000179 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000180
Nate Begemanb3923212005-08-04 23:24:19 +0000181 // Otherwise if all of the incoming values are the same for the PHI, replace
182 // the PHI node with the incoming value.
183 //
184 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000185 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000186 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000187 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000188 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000189 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000190 if (InVal && getIncomingValue(i) != InVal)
191 return 0; // Not the same, bail out.
Chris Lattner7d08da62009-09-21 22:27:34 +0000192 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000193 }
Nate Begemanb3923212005-08-04 23:24:19 +0000194
195 // The only case that could cause InVal to be null is if we have a PHI node
196 // that only has entries for itself. In this case, there is no entry into the
197 // loop, so kill the PHI.
198 //
Owen Andersonb292b8c2009-07-30 23:03:37 +0000199 if (InVal == 0) InVal = UndefValue::get(getType());
Nate Begemanb3923212005-08-04 23:24:19 +0000200
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000201 // If we have a PHI node like phi(X, undef, X), where X is defined by some
202 // instruction, we cannot always return X as the result of the PHI node. Only
203 // do this if X is not an instruction (thus it must dominate the PHI block),
204 // or if the client is prepared to deal with this possibility.
Chris Lattner7d08da62009-09-21 22:27:34 +0000205 if (!HasUndefInput || !isa<Instruction>(InVal))
206 return InVal;
207
208 Instruction *IV = cast<Instruction>(InVal);
209 if (DT) {
210 // We have a DominatorTree. Do a precise test.
211 if (!DT->dominates(IV, this))
212 return 0;
213 } else {
214 // If it is in the entry block, it obviously dominates everything.
215 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
216 isa<InvokeInst>(IV))
217 return 0; // Cannot guarantee that InVal dominates this PHINode.
218 }
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000219
Nate Begemanb3923212005-08-04 23:24:19 +0000220 // All of the incoming values are the same, return the value now.
221 return InVal;
222}
223
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000224
225//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000226// CallInst Implementation
227//===----------------------------------------------------------------------===//
228
Gordon Henriksen14a55692007-12-10 02:14:30 +0000229CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000230}
231
Chris Lattner054ba2c2007-02-13 00:58:44 +0000232void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000233 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000234 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000235
Misha Brukmanb1c93172005-04-21 23:48:37 +0000236 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000237 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000238 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000239
Chris Lattner054ba2c2007-02-13 00:58:44 +0000240 assert((NumParams == FTy->getNumParams() ||
241 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000242 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000243 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000244 assert((i >= FTy->getNumParams() ||
245 FTy->getParamType(i) == Params[i]->getType()) &&
246 "Calling a function with a bad signature!");
Gabor Greif6d673952010-07-16 09:38:02 +0000247 OperandList[i] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000248 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000249}
250
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000251void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000252 assert(NumOperands == 3 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000253 Op<-1>() = Func;
254 Op<0>() = Actual1;
255 Op<1>() = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000256
257 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000258 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000259 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000260
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000261 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000262 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000263 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000264 assert((0 >= FTy->getNumParams() ||
265 FTy->getParamType(0) == Actual1->getType()) &&
266 "Calling a function with a bad signature!");
267 assert((1 >= FTy->getNumParams() ||
268 FTy->getParamType(1) == Actual2->getType()) &&
269 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000270}
271
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000272void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000273 assert(NumOperands == 2 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000274 Op<-1>() = Func;
275 Op<0>() = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000276
277 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000278 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000279 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000280
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000281 assert((FTy->getNumParams() == 1 ||
282 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000283 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000284 assert((0 == FTy->getNumParams() ||
285 FTy->getParamType(0) == Actual->getType()) &&
286 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000287}
288
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000289void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000290 assert(NumOperands == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000291 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000292
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000293 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000294 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000295 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000296
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000297 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000298}
299
Daniel Dunbar4975db62009-07-25 04:41:11 +0000300CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000301 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000302 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
303 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000304 Instruction::Call,
305 OperandTraits<CallInst>::op_end(this) - 2,
306 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000307 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000308 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000309}
310
Daniel Dunbar4975db62009-07-25 04:41:11 +0000311CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000312 BasicBlock *InsertAtEnd)
313 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
314 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000315 Instruction::Call,
316 OperandTraits<CallInst>::op_end(this) - 2,
317 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000318 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000319 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000320}
Daniel Dunbar4975db62009-07-25 04:41:11 +0000321CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000322 Instruction *InsertBefore)
323 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
324 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000325 Instruction::Call,
326 OperandTraits<CallInst>::op_end(this) - 1,
327 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000328 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000329 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000330}
331
Daniel Dunbar4975db62009-07-25 04:41:11 +0000332CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000333 BasicBlock *InsertAtEnd)
334 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
335 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000336 Instruction::Call,
337 OperandTraits<CallInst>::op_end(this) - 1,
338 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000339 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000340 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000341}
342
Misha Brukmanb1c93172005-04-21 23:48:37 +0000343CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000344 : Instruction(CI.getType(), Instruction::Call,
345 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000346 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000347 setAttributes(CI.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000348 setTailCall(CI.isTailCall());
349 setCallingConv(CI.getCallingConv());
350
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000351 Use *OL = OperandList;
352 Use *InOL = CI.OperandList;
353 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000354 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000355 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000356}
357
Devang Patel4c758ea2008-09-25 21:00:45 +0000358void CallInst::addAttribute(unsigned i, Attributes attr) {
359 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000360 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000361 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000362}
363
Devang Patel4c758ea2008-09-25 21:00:45 +0000364void CallInst::removeAttribute(unsigned i, Attributes attr) {
365 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000366 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000367 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000368}
369
Devang Patelba3fa6c2008-09-23 23:03:40 +0000370bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000371 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000372 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000373 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000374 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000375 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000376}
377
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000378/// IsConstantOne - Return true only if val is constant int 1
379static bool IsConstantOne(Value *val) {
380 assert(val && "IsConstantOne does not work with NULL val");
381 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
382}
383
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000384static Instruction *createMalloc(Instruction *InsertBefore,
385 BasicBlock *InsertAtEnd, const Type *IntPtrTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000386 const Type *AllocTy, Value *AllocSize,
387 Value *ArraySize, Function *MallocF,
388 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000389 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000390 "createMalloc needs either InsertBefore or InsertAtEnd");
391
392 // malloc(type) becomes:
393 // bitcast (i8* malloc(typeSize)) to type*
394 // malloc(type, arraySize) becomes:
395 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000396 if (!ArraySize)
397 ArraySize = ConstantInt::get(IntPtrTy, 1);
398 else if (ArraySize->getType() != IntPtrTy) {
399 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000400 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
401 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000402 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000403 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
404 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000405 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000406
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000407 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000408 if (IsConstantOne(AllocSize)) {
409 AllocSize = ArraySize; // Operand * 1 = Operand
410 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
411 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
412 false /*ZExt*/);
413 // Malloc arg is constant product of type size and array size
414 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
415 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000416 // Multiply type size by the array size...
417 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000418 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
419 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000420 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000421 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
422 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000423 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000424 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000425
Victor Hernandez788eaab2009-09-18 19:20:02 +0000426 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000427 // Create the call to Malloc.
428 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
429 Module* M = BB->getParent()->getParent();
Duncan Sands9ed7b162009-10-06 15:40:36 +0000430 const Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000431 Value *MallocFunc = MallocF;
432 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000433 // prototype malloc as "void *malloc(size_t)"
Victor Hernandezbb336a12009-11-10 19:53:28 +0000434 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000435 const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000436 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000437 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000438 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000439 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000440 Result = MCall;
441 if (Result->getType() != AllocPtrType)
442 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000443 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000444 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000445 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000446 Result = MCall;
447 if (Result->getType() != AllocPtrType) {
448 InsertAtEnd->getInstList().push_back(MCall);
449 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000450 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000451 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000452 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000453 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000454 if (Function *F = dyn_cast<Function>(MallocFunc)) {
455 MCall->setCallingConv(F->getCallingConv());
456 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
457 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000458 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000459
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000460 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000461}
462
463/// CreateMalloc - Generate the IR for a call to malloc:
464/// 1. Compute the malloc call's argument as the specified type's size,
465/// possibly multiplied by the array size if the array size is not
466/// constant 1.
467/// 2. Call malloc with that argument.
468/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000469Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
470 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000471 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000472 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000473 const Twine &Name) {
474 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000475 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000476}
477
478/// CreateMalloc - Generate the IR for a call to malloc:
479/// 1. Compute the malloc call's argument as the specified type's size,
480/// possibly multiplied by the array size if the array size is not
481/// constant 1.
482/// 2. Call malloc with that argument.
483/// 3. Bitcast the result of the malloc call to the specified type.
484/// Note: This function does not add the bitcast to the basic block, that is the
485/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000486Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
487 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000488 Value *AllocSize, Value *ArraySize,
489 Function *MallocF, const Twine &Name) {
490 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000491 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000492}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000493
Victor Hernandeze2971492009-10-24 04:23:03 +0000494static Instruction* createFree(Value* Source, Instruction *InsertBefore,
495 BasicBlock *InsertAtEnd) {
496 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
497 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000498 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000499 "Can not free something of nonpointer type!");
500
501 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
502 Module* M = BB->getParent()->getParent();
503
504 const Type *VoidTy = Type::getVoidTy(M->getContext());
505 const Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
506 // prototype free as "void free(void*)"
Chris Lattner2156c222009-11-09 07:12:01 +0000507 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000508 CallInst* Result = NULL;
509 Value *PtrCast = Source;
510 if (InsertBefore) {
511 if (Source->getType() != IntPtrTy)
512 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
513 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
514 } else {
515 if (Source->getType() != IntPtrTy)
516 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
517 Result = CallInst::Create(FreeFunc, PtrCast, "");
518 }
519 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000520 if (Function *F = dyn_cast<Function>(FreeFunc))
521 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000522
523 return Result;
524}
525
526/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000527Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
528 return createFree(Source, InsertBefore, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000529}
530
531/// CreateFree - Generate the IR for a call to the builtin free function.
532/// Note: This function does not add the call to the basic block, that is the
533/// responsibility of the caller.
534Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
535 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
536 assert(FreeCall && "CreateFree did not create a CallInst");
537 return FreeCall;
538}
539
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000540//===----------------------------------------------------------------------===//
541// InvokeInst Implementation
542//===----------------------------------------------------------------------===//
543
544void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000545 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000546 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000547 Op<-3>() = Fn;
548 Op<-2>() = IfNormal;
549 Op<-1>() = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000550 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000551 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000552 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000553
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000554 assert(((NumArgs == FTy->getNumParams()) ||
555 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000556 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000557
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000558 Use *OL = OperandList;
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000559 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000560 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000561 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000562 "Invoking a function with a bad signature!");
563
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000564 OL[i] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000565 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000566}
567
Misha Brukmanb1c93172005-04-21 23:48:37 +0000568InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000569 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000570 OperandTraits<InvokeInst>::op_end(this)
571 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000572 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000573 setAttributes(II.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000574 setCallingConv(II.getCallingConv());
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000575 Use *OL = OperandList, *InOL = II.OperandList;
576 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000577 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000578 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000579}
580
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000581BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
582 return getSuccessor(idx);
583}
584unsigned InvokeInst::getNumSuccessorsV() const {
585 return getNumSuccessors();
586}
587void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
588 return setSuccessor(idx, B);
589}
590
Devang Patelba3fa6c2008-09-23 23:03:40 +0000591bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000592 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000593 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000594 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000595 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000596 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000597}
598
Devang Patel4c758ea2008-09-25 21:00:45 +0000599void InvokeInst::addAttribute(unsigned i, Attributes attr) {
600 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000601 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000602 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000603}
604
Devang Patel4c758ea2008-09-25 21:00:45 +0000605void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
606 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000607 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000608 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000609}
610
Duncan Sands5208d1a2007-11-28 17:07:01 +0000611
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000612//===----------------------------------------------------------------------===//
613// ReturnInst Implementation
614//===----------------------------------------------------------------------===//
615
Chris Lattner2195fc42007-02-24 00:55:48 +0000616ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000617 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000618 OperandTraits<ReturnInst>::op_end(this) -
619 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000620 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000621 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000622 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000623 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000624}
625
Owen Anderson55f1c092009-08-13 21:58:54 +0000626ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
627 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000628 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
629 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000630 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000631 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000632}
Owen Anderson55f1c092009-08-13 21:58:54 +0000633ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
634 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000635 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
636 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000637 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000638 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000639}
Owen Anderson55f1c092009-08-13 21:58:54 +0000640ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
641 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000642 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000643}
644
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000645unsigned ReturnInst::getNumSuccessorsV() const {
646 return getNumSuccessors();
647}
648
Devang Patelae682fb2008-02-26 17:56:20 +0000649/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
650/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000651void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000652 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000653}
654
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000655BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000656 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000657 return 0;
658}
659
Devang Patel59643e52008-02-23 00:35:18 +0000660ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000661}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000662
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000663//===----------------------------------------------------------------------===//
664// UnwindInst Implementation
665//===----------------------------------------------------------------------===//
666
Owen Anderson55f1c092009-08-13 21:58:54 +0000667UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
668 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
669 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000670}
Owen Anderson55f1c092009-08-13 21:58:54 +0000671UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
672 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
673 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000674}
675
676
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000677unsigned UnwindInst::getNumSuccessorsV() const {
678 return getNumSuccessors();
679}
680
681void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000682 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000683}
684
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000685BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000686 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000687 return 0;
688}
689
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000690//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000691// UnreachableInst Implementation
692//===----------------------------------------------------------------------===//
693
Owen Anderson55f1c092009-08-13 21:58:54 +0000694UnreachableInst::UnreachableInst(LLVMContext &Context,
695 Instruction *InsertBefore)
696 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
697 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000698}
Owen Anderson55f1c092009-08-13 21:58:54 +0000699UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
700 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
701 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000702}
703
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000704unsigned UnreachableInst::getNumSuccessorsV() const {
705 return getNumSuccessors();
706}
707
708void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000709 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000710}
711
712BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000713 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000714 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000715}
716
717//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000718// BranchInst Implementation
719//===----------------------------------------------------------------------===//
720
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000721void BranchInst::AssertOK() {
722 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000723 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000724 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000725}
726
Chris Lattner2195fc42007-02-24 00:55:48 +0000727BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000728 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000729 OperandTraits<BranchInst>::op_end(this) - 1,
730 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000731 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000732 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000733}
734BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
735 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000736 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000737 OperandTraits<BranchInst>::op_end(this) - 3,
738 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000739 Op<-1>() = IfTrue;
740 Op<-2>() = IfFalse;
741 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000742#ifndef NDEBUG
743 AssertOK();
744#endif
745}
746
747BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000748 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000749 OperandTraits<BranchInst>::op_end(this) - 1,
750 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000751 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000752 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000753}
754
755BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
756 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000757 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000758 OperandTraits<BranchInst>::op_end(this) - 3,
759 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000760 Op<-1>() = IfTrue;
761 Op<-2>() = IfFalse;
762 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000763#ifndef NDEBUG
764 AssertOK();
765#endif
766}
767
768
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000769BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000770 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000771 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
772 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000773 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000774 if (BI.getNumOperands() != 1) {
775 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000776 Op<-3>() = BI.Op<-3>();
777 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000778 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000779 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000780}
781
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000782
783Use* Use::getPrefix() {
784 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
785 if (PotentialPrefix.getOpaqueValue())
786 return 0;
787
788 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
789}
790
791BranchInst::~BranchInst() {
792 if (NumOperands == 1) {
793 if (Use *Prefix = OperandList->getPrefix()) {
794 Op<-1>() = 0;
795 //
796 // mark OperandList to have a special value for scrutiny
797 // by baseclass destructors and operator delete
798 OperandList = Prefix;
799 } else {
800 NumOperands = 3;
801 OperandList = op_begin();
802 }
803 }
804}
805
806
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000807BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
808 return getSuccessor(idx);
809}
810unsigned BranchInst::getNumSuccessorsV() const {
811 return getNumSuccessors();
812}
813void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
814 setSuccessor(idx, B);
815}
816
817
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000818//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000819// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000820//===----------------------------------------------------------------------===//
821
Owen Andersonb6b25302009-07-14 23:09:55 +0000822static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000823 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000824 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000825 else {
826 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000827 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000828 assert(Amt->getType()->isIntegerTy() &&
829 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000830 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000831 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000832}
833
Victor Hernandez8acf2952009-10-23 21:09:37 +0000834AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
835 const Twine &Name, Instruction *InsertBefore)
836 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
837 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
838 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000839 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000840 setName(Name);
841}
842
843AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
844 const Twine &Name, BasicBlock *InsertAtEnd)
845 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
846 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
847 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000848 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000849 setName(Name);
850}
851
852AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
853 Instruction *InsertBefore)
854 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
855 getAISize(Ty->getContext(), 0), InsertBefore) {
856 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000857 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000858 setName(Name);
859}
860
861AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
862 BasicBlock *InsertAtEnd)
863 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
864 getAISize(Ty->getContext(), 0), InsertAtEnd) {
865 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000866 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000867 setName(Name);
868}
869
870AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
871 const Twine &Name, Instruction *InsertBefore)
872 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000873 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000874 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000875 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000876 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000877}
878
Victor Hernandez8acf2952009-10-23 21:09:37 +0000879AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
880 const Twine &Name, BasicBlock *InsertAtEnd)
881 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000882 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000883 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000884 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000885 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000886}
887
Gordon Henriksen14a55692007-12-10 02:14:30 +0000888// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000889AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000890}
891
Victor Hernandez8acf2952009-10-23 21:09:37 +0000892void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000893 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000894 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohmanaa583d72008-03-24 16:55:58 +0000895 assert(getAlignment() == Align && "Alignment representation error!");
896}
897
Victor Hernandez8acf2952009-10-23 21:09:37 +0000898bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000899 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
900 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000901 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000902}
903
Victor Hernandez8acf2952009-10-23 21:09:37 +0000904const Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000905 return getType()->getElementType();
906}
907
Chris Lattner8b291e62008-11-26 02:54:17 +0000908/// isStaticAlloca - Return true if this alloca is in the entry block of the
909/// function and is a constant size. If so, the code generator will fold it
910/// into the prolog/epilog code, so it is basically free.
911bool AllocaInst::isStaticAlloca() const {
912 // Must be constant size.
913 if (!isa<ConstantInt>(getArraySize())) return false;
914
915 // Must be in the entry block.
916 const BasicBlock *Parent = getParent();
917 return Parent == &Parent->getParent()->front();
918}
919
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000920//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000921// LoadInst Implementation
922//===----------------------------------------------------------------------===//
923
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000924void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +0000925 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000926 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000927}
928
Daniel Dunbar4975db62009-07-25 04:41:11 +0000929LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000930 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000931 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000932 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000933 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000934 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000935 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000936}
937
Daniel Dunbar4975db62009-07-25 04:41:11 +0000938LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000939 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000940 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000941 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000942 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000943 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000944 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000945}
946
Daniel Dunbar4975db62009-07-25 04:41:11 +0000947LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000948 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000949 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000950 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000951 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000952 setAlignment(0);
953 AssertOK();
954 setName(Name);
955}
956
Daniel Dunbar4975db62009-07-25 04:41:11 +0000957LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000958 unsigned Align, Instruction *InsertBef)
959 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
960 Load, Ptr, InsertBef) {
961 setVolatile(isVolatile);
962 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000963 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000964 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000965}
966
Daniel Dunbar4975db62009-07-25 04:41:11 +0000967LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000968 unsigned Align, BasicBlock *InsertAE)
969 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
970 Load, Ptr, InsertAE) {
971 setVolatile(isVolatile);
972 setAlignment(Align);
973 AssertOK();
974 setName(Name);
975}
976
Daniel Dunbar4975db62009-07-25 04:41:11 +0000977LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000978 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000979 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000980 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000981 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000982 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000983 AssertOK();
984 setName(Name);
985}
986
Daniel Dunbar27096822009-08-11 18:11:15 +0000987
988
989LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
990 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
991 Load, Ptr, InsertBef) {
992 setVolatile(false);
993 setAlignment(0);
994 AssertOK();
995 if (Name && Name[0]) setName(Name);
996}
997
998LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
999 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1000 Load, Ptr, InsertAE) {
1001 setVolatile(false);
1002 setAlignment(0);
1003 AssertOK();
1004 if (Name && Name[0]) setName(Name);
1005}
1006
1007LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1008 Instruction *InsertBef)
1009: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1010 Load, Ptr, InsertBef) {
1011 setVolatile(isVolatile);
1012 setAlignment(0);
1013 AssertOK();
1014 if (Name && Name[0]) setName(Name);
1015}
1016
1017LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1018 BasicBlock *InsertAE)
1019 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1020 Load, Ptr, InsertAE) {
1021 setVolatile(isVolatile);
1022 setAlignment(0);
1023 AssertOK();
1024 if (Name && Name[0]) setName(Name);
1025}
1026
Christopher Lamb84485702007-04-22 19:24:39 +00001027void LoadInst::setAlignment(unsigned Align) {
1028 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001029 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1030 ((Log2_32(Align)+1)<<1));
Christopher Lamb84485702007-04-22 19:24:39 +00001031}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001032
1033//===----------------------------------------------------------------------===//
1034// StoreInst Implementation
1035//===----------------------------------------------------------------------===//
1036
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001037void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001038 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001039 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001040 "Ptr must have pointer type!");
1041 assert(getOperand(0)->getType() ==
1042 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001043 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001044}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001045
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001046
1047StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001048 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001049 OperandTraits<StoreInst>::op_begin(this),
1050 OperandTraits<StoreInst>::operands(this),
1051 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001052 Op<0>() = val;
1053 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001054 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001055 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001056 AssertOK();
1057}
1058
1059StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001060 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001061 OperandTraits<StoreInst>::op_begin(this),
1062 OperandTraits<StoreInst>::operands(this),
1063 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001064 Op<0>() = val;
1065 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001066 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001067 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001068 AssertOK();
1069}
1070
Misha Brukmanb1c93172005-04-21 23:48:37 +00001071StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001072 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001073 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001074 OperandTraits<StoreInst>::op_begin(this),
1075 OperandTraits<StoreInst>::operands(this),
1076 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001077 Op<0>() = val;
1078 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001079 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001080 setAlignment(0);
1081 AssertOK();
1082}
1083
1084StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1085 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001086 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001087 OperandTraits<StoreInst>::op_begin(this),
1088 OperandTraits<StoreInst>::operands(this),
1089 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001090 Op<0>() = val;
1091 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001092 setVolatile(isVolatile);
1093 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001094 AssertOK();
1095}
1096
Misha Brukmanb1c93172005-04-21 23:48:37 +00001097StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001098 unsigned Align, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001099 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001100 OperandTraits<StoreInst>::op_begin(this),
1101 OperandTraits<StoreInst>::operands(this),
1102 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001103 Op<0>() = val;
1104 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001105 setVolatile(isVolatile);
1106 setAlignment(Align);
1107 AssertOK();
1108}
1109
1110StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001111 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001112 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001113 OperandTraits<StoreInst>::op_begin(this),
1114 OperandTraits<StoreInst>::operands(this),
1115 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001116 Op<0>() = val;
1117 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001118 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001119 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001120 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001121}
1122
Christopher Lamb84485702007-04-22 19:24:39 +00001123void StoreInst::setAlignment(unsigned Align) {
1124 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001125 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1126 ((Log2_32(Align)+1) << 1));
Christopher Lamb84485702007-04-22 19:24:39 +00001127}
1128
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001129//===----------------------------------------------------------------------===//
1130// GetElementPtrInst Implementation
1131//===----------------------------------------------------------------------===//
1132
Christopher Lambedf07882007-12-17 01:12:55 +00001133static unsigned retrieveAddrSpace(const Value *Val) {
1134 return cast<PointerType>(Val->getType())->getAddressSpace();
1135}
1136
Gabor Greif21ba1842008-06-06 20:28:12 +00001137void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001138 const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001139 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1140 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001141 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001142
Chris Lattner79807c3d2007-01-31 19:47:18 +00001143 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001144 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001145
1146 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001147}
1148
Daniel Dunbar4975db62009-07-25 04:41:11 +00001149void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001150 assert(NumOperands == 2 && "NumOperands not initialized?");
1151 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001152 OL[0] = Ptr;
1153 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001154
1155 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001156}
1157
Gabor Greiff6caff662008-05-10 08:32:32 +00001158GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001159 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001160 OperandTraits<GetElementPtrInst>::op_end(this)
1161 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001162 GEPI.getNumOperands()) {
1163 Use *OL = OperandList;
1164 Use *GEPIOL = GEPI.OperandList;
1165 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001166 OL[i] = GEPIOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001167 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001168}
1169
Chris Lattner82981202005-05-03 05:43:30 +00001170GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001171 const Twine &Name, Instruction *InBe)
Owen Anderson4056ca92009-07-29 22:17:13 +00001172 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001173 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001174 GetElementPtr,
1175 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1176 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001177 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001178}
1179
1180GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001181 const Twine &Name, BasicBlock *IAE)
Owen Anderson4056ca92009-07-29 22:17:13 +00001182 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001183 checkType(getIndexedType(Ptr->getType(),Idx)),
1184 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001185 GetElementPtr,
1186 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1187 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001188 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001189}
1190
Chris Lattner6090a422009-03-09 04:46:40 +00001191/// getIndexedType - Returns the type of the element that would be accessed with
1192/// a gep instruction with the specified parameters.
1193///
1194/// The Idxs pointer should point to a continuous piece of memory containing the
1195/// indices, either as Value* or uint64_t.
1196///
1197/// A null type is returned if the indices are invalid for the specified
1198/// pointer type.
1199///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001200template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001201static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1202 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001203 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1204 if (!PTy) return 0; // Type isn't a pointer type!
1205 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001206
Chris Lattner6090a422009-03-09 04:46:40 +00001207 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001208 if (NumIdx == 0)
1209 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001210
1211 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001212 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1213 // that contain opaque types) under the assumption that it will be resolved to
1214 // a sane type later.
1215 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001216 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001217
Dan Gohman1ecaf452008-05-31 00:58:22 +00001218 unsigned CurIdx = 1;
1219 for (; CurIdx != NumIdx; ++CurIdx) {
1220 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001221 if (!CT || CT->isPointerTy()) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001222 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001223 if (!CT->indexValid(Index)) return 0;
1224 Agg = CT->getTypeAtIndex(Index);
1225
1226 // If the new type forwards to another type, then it is in the middle
1227 // of being refined to another type (and hence, may have dropped all
1228 // references to what it was using before). So, use the new forwarded
1229 // type.
1230 if (const Type *Ty = Agg->getForwardedType())
1231 Agg = Ty;
1232 }
1233 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001234}
1235
Matthijs Kooijman04468622008-07-29 08:46:11 +00001236const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1237 Value* const *Idxs,
1238 unsigned NumIdx) {
1239 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1240}
1241
1242const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1243 uint64_t const *Idxs,
1244 unsigned NumIdx) {
1245 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1246}
1247
Chris Lattner82981202005-05-03 05:43:30 +00001248const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1249 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1250 if (!PTy) return 0; // Type isn't a pointer type!
1251
1252 // Check the pointer index.
1253 if (!PTy->indexValid(Idx)) return 0;
1254
Chris Lattnerc2233332005-05-03 16:44:45 +00001255 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001256}
1257
Chris Lattner45f15572007-04-14 00:12:57 +00001258
1259/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1260/// zeros. If so, the result pointer and the first operand have the same
1261/// value, just potentially different types.
1262bool GetElementPtrInst::hasAllZeroIndices() const {
1263 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1264 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1265 if (!CI->isZero()) return false;
1266 } else {
1267 return false;
1268 }
1269 }
1270 return true;
1271}
1272
Chris Lattner27058292007-04-27 20:35:56 +00001273/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1274/// constant integers. If so, the result pointer and the first operand have
1275/// a constant offset between them.
1276bool GetElementPtrInst::hasAllConstantIndices() const {
1277 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1278 if (!isa<ConstantInt>(getOperand(i)))
1279 return false;
1280 }
1281 return true;
1282}
1283
Dan Gohman1b849082009-09-07 23:54:19 +00001284void GetElementPtrInst::setIsInBounds(bool B) {
1285 cast<GEPOperator>(this)->setIsInBounds(B);
1286}
Chris Lattner45f15572007-04-14 00:12:57 +00001287
Nick Lewycky28a5f252009-09-27 21:33:04 +00001288bool GetElementPtrInst::isInBounds() const {
1289 return cast<GEPOperator>(this)->isInBounds();
1290}
1291
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001292//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001293// ExtractElementInst Implementation
1294//===----------------------------------------------------------------------===//
1295
1296ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001297 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001298 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001299 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001300 ExtractElement,
1301 OperandTraits<ExtractElementInst>::op_begin(this),
1302 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001303 assert(isValidOperands(Val, Index) &&
1304 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001305 Op<0>() = Val;
1306 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001307 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001308}
1309
1310ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001311 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001312 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001313 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001314 ExtractElement,
1315 OperandTraits<ExtractElementInst>::op_begin(this),
1316 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001317 assert(isValidOperands(Val, Index) &&
1318 "Invalid extractelement instruction operands!");
1319
Gabor Greif2d3024d2008-05-26 21:33:52 +00001320 Op<0>() = Val;
1321 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001322 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001323}
1324
Chris Lattner65511ff2006-10-05 06:24:58 +00001325
Chris Lattner54865b32006-04-08 04:05:48 +00001326bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001327 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
Chris Lattner54865b32006-04-08 04:05:48 +00001328 return false;
1329 return true;
1330}
1331
1332
Robert Bocchino23004482006-01-10 19:05:34 +00001333//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001334// InsertElementInst Implementation
1335//===----------------------------------------------------------------------===//
1336
Chris Lattner54865b32006-04-08 04:05:48 +00001337InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001338 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001339 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001340 : Instruction(Vec->getType(), InsertElement,
1341 OperandTraits<InsertElementInst>::op_begin(this),
1342 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001343 assert(isValidOperands(Vec, Elt, Index) &&
1344 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001345 Op<0>() = Vec;
1346 Op<1>() = Elt;
1347 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001348 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001349}
1350
Chris Lattner54865b32006-04-08 04:05:48 +00001351InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001352 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001353 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001354 : Instruction(Vec->getType(), InsertElement,
1355 OperandTraits<InsertElementInst>::op_begin(this),
1356 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001357 assert(isValidOperands(Vec, Elt, Index) &&
1358 "Invalid insertelement instruction operands!");
1359
Gabor Greif2d3024d2008-05-26 21:33:52 +00001360 Op<0>() = Vec;
1361 Op<1>() = Elt;
1362 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001363 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001364}
1365
Chris Lattner54865b32006-04-08 04:05:48 +00001366bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1367 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001368 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001369 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001370
Reid Spencerd84d35b2007-02-15 02:26:10 +00001371 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001372 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001373
Duncan Sands9dff9be2010-02-15 16:12:20 +00001374 if (!Index->getType()->isIntegerTy(32))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001375 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001376 return true;
1377}
1378
1379
Robert Bocchinoca27f032006-01-17 20:07:22 +00001380//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001381// ShuffleVectorInst Implementation
1382//===----------------------------------------------------------------------===//
1383
1384ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001385 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001386 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001387: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001388 cast<VectorType>(Mask->getType())->getNumElements()),
1389 ShuffleVector,
1390 OperandTraits<ShuffleVectorInst>::op_begin(this),
1391 OperandTraits<ShuffleVectorInst>::operands(this),
1392 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001393 assert(isValidOperands(V1, V2, Mask) &&
1394 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001395 Op<0>() = V1;
1396 Op<1>() = V2;
1397 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001398 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001399}
1400
1401ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001402 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001403 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001404: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1405 cast<VectorType>(Mask->getType())->getNumElements()),
1406 ShuffleVector,
1407 OperandTraits<ShuffleVectorInst>::op_begin(this),
1408 OperandTraits<ShuffleVectorInst>::operands(this),
1409 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001410 assert(isValidOperands(V1, V2, Mask) &&
1411 "Invalid shuffle vector instruction operands!");
1412
Gabor Greif2d3024d2008-05-26 21:33:52 +00001413 Op<0>() = V1;
1414 Op<1>() = V2;
1415 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001416 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001417}
1418
Mon P Wang25f01062008-11-10 04:46:22 +00001419bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001420 const Value *Mask) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001421 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001422 return false;
1423
1424 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1425 if (!isa<Constant>(Mask) || MaskTy == 0 ||
Duncan Sands9dff9be2010-02-15 16:12:20 +00001426 !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001427 return false;
1428 return true;
1429}
1430
Chris Lattnerf724e342008-03-02 05:28:33 +00001431/// getMaskValue - Return the index from the shuffle mask for the specified
1432/// output result. This is either -1 if the element is undef or a number less
1433/// than 2*numelements.
1434int ShuffleVectorInst::getMaskValue(unsigned i) const {
1435 const Constant *Mask = cast<Constant>(getOperand(2));
1436 if (isa<UndefValue>(Mask)) return -1;
1437 if (isa<ConstantAggregateZero>(Mask)) return 0;
1438 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1439 assert(i < MaskCV->getNumOperands() && "Index out of range");
1440
1441 if (isa<UndefValue>(MaskCV->getOperand(i)))
1442 return -1;
1443 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1444}
1445
Dan Gohman12fce772008-05-15 19:50:34 +00001446//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001447// InsertValueInst Class
1448//===----------------------------------------------------------------------===//
1449
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001450void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001451 unsigned NumIdx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001452 assert(NumOperands == 2 && "NumOperands not initialized?");
1453 Op<0>() = Agg;
1454 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001455
Dan Gohmandd41bba2010-06-21 19:47:52 +00001456 Indices.append(Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001457 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001458}
1459
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001460void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001461 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001462 assert(NumOperands == 2 && "NumOperands not initialized?");
1463 Op<0>() = Agg;
1464 Op<1>() = Val;
1465
1466 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001467 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001468}
1469
1470InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001471 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001472 OperandTraits<InsertValueInst>::op_begin(this), 2),
1473 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001474 Op<0>() = IVI.getOperand(0);
1475 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001476 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001477}
1478
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001479InsertValueInst::InsertValueInst(Value *Agg,
1480 Value *Val,
1481 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001482 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001483 Instruction *InsertBefore)
1484 : Instruction(Agg->getType(), InsertValue,
1485 OperandTraits<InsertValueInst>::op_begin(this),
1486 2, InsertBefore) {
1487 init(Agg, Val, Idx, Name);
1488}
1489
1490InsertValueInst::InsertValueInst(Value *Agg,
1491 Value *Val,
1492 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001493 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001494 BasicBlock *InsertAtEnd)
1495 : Instruction(Agg->getType(), InsertValue,
1496 OperandTraits<InsertValueInst>::op_begin(this),
1497 2, InsertAtEnd) {
1498 init(Agg, Val, Idx, Name);
1499}
1500
Dan Gohman0752bff2008-05-23 00:36:11 +00001501//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001502// ExtractValueInst Class
1503//===----------------------------------------------------------------------===//
1504
Gabor Greifcbcc4952008-06-06 21:06:32 +00001505void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001506 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001507 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001508
Dan Gohmandd41bba2010-06-21 19:47:52 +00001509 Indices.append(Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001510 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001511}
1512
Daniel Dunbar4975db62009-07-25 04:41:11 +00001513void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001514 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001515
1516 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001517 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001518}
1519
1520ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001521 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001522 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001523 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001524}
1525
Dan Gohman12fce772008-05-15 19:50:34 +00001526// getIndexedType - Returns the type of the element that would be extracted
1527// with an extractvalue instruction with the specified parameters.
1528//
1529// A null type is returned if the indices are invalid for the specified
1530// pointer type.
1531//
1532const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001533 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001534 unsigned NumIdx) {
1535 unsigned CurIdx = 0;
1536 for (; CurIdx != NumIdx; ++CurIdx) {
1537 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001538 if (!CT || CT->isPointerTy() || CT->isVectorTy()) return 0;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001539 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001540 if (!CT->indexValid(Index)) return 0;
1541 Agg = CT->getTypeAtIndex(Index);
1542
1543 // If the new type forwards to another type, then it is in the middle
1544 // of being refined to another type (and hence, may have dropped all
1545 // references to what it was using before). So, use the new forwarded
1546 // type.
1547 if (const Type *Ty = Agg->getForwardedType())
1548 Agg = Ty;
1549 }
1550 return CurIdx == NumIdx ? Agg : 0;
1551}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001552
Dan Gohman4a3125b2008-06-17 21:07:55 +00001553const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001554 unsigned Idx) {
1555 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001556}
1557
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001558//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001559// BinaryOperator Class
1560//===----------------------------------------------------------------------===//
1561
Chris Lattner2195fc42007-02-24 00:55:48 +00001562BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001563 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001564 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001565 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001566 OperandTraits<BinaryOperator>::op_begin(this),
1567 OperandTraits<BinaryOperator>::operands(this),
1568 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001569 Op<0>() = S1;
1570 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001571 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001572 setName(Name);
1573}
1574
1575BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001576 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001577 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001578 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001579 OperandTraits<BinaryOperator>::op_begin(this),
1580 OperandTraits<BinaryOperator>::operands(this),
1581 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001582 Op<0>() = S1;
1583 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001584 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001585 setName(Name);
1586}
1587
1588
1589void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001590 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001591 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001592 assert(LHS->getType() == RHS->getType() &&
1593 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001594#ifndef NDEBUG
1595 switch (iType) {
1596 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001597 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001598 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001599 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001600 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001601 "Tried to create an integer operation on a non-integer type!");
1602 break;
1603 case FAdd: case FSub:
1604 case FMul:
1605 assert(getType() == LHS->getType() &&
1606 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001607 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001608 "Tried to create a floating-point operation on a "
1609 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001610 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001611 case UDiv:
1612 case SDiv:
1613 assert(getType() == LHS->getType() &&
1614 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001615 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001616 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001617 "Incorrect operand type (not integer) for S/UDIV");
1618 break;
1619 case FDiv:
1620 assert(getType() == LHS->getType() &&
1621 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001622 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001623 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001624 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001625 case URem:
1626 case SRem:
1627 assert(getType() == LHS->getType() &&
1628 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001629 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001630 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001631 "Incorrect operand type (not integer) for S/UREM");
1632 break;
1633 case FRem:
1634 assert(getType() == LHS->getType() &&
1635 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001636 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001637 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001638 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001639 case Shl:
1640 case LShr:
1641 case AShr:
1642 assert(getType() == LHS->getType() &&
1643 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001644 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001645 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001646 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001647 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001648 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001649 case And: case Or:
1650 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001651 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001652 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001653 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001654 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001655 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001656 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001657 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001658 default:
1659 break;
1660 }
1661#endif
1662}
1663
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001664BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001665 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001666 Instruction *InsertBefore) {
1667 assert(S1->getType() == S2->getType() &&
1668 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001669 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001670}
1671
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001672BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001673 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001674 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001675 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001676 InsertAtEnd->getInstList().push_back(Res);
1677 return Res;
1678}
1679
Dan Gohman5476cfd2009-08-12 16:23:25 +00001680BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001681 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001682 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001683 return new BinaryOperator(Instruction::Sub,
1684 zero, Op,
1685 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001686}
1687
Dan Gohman5476cfd2009-08-12 16:23:25 +00001688BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001689 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001690 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001691 return new BinaryOperator(Instruction::Sub,
1692 zero, Op,
1693 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001694}
1695
Dan Gohman4ab44202009-12-18 02:58:50 +00001696BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1697 Instruction *InsertBefore) {
1698 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1699 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1700}
1701
1702BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1703 BasicBlock *InsertAtEnd) {
1704 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1705 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1706}
1707
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001708BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1709 Instruction *InsertBefore) {
1710 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1711 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1712}
1713
1714BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1715 BasicBlock *InsertAtEnd) {
1716 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1717 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1718}
1719
Dan Gohman5476cfd2009-08-12 16:23:25 +00001720BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001721 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001722 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001723 return new BinaryOperator(Instruction::FSub,
1724 zero, Op,
1725 Op->getType(), Name, InsertBefore);
1726}
1727
Dan Gohman5476cfd2009-08-12 16:23:25 +00001728BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001729 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001730 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001731 return new BinaryOperator(Instruction::FSub,
1732 zero, Op,
1733 Op->getType(), Name, InsertAtEnd);
1734}
1735
Dan Gohman5476cfd2009-08-12 16:23:25 +00001736BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001737 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001738 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001739 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001740 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001741 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001742 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001743 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001744 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001745 }
1746
1747 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001748 Op->getType(), Name, InsertBefore);
1749}
1750
Dan Gohman5476cfd2009-08-12 16:23:25 +00001751BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001752 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001753 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001754 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001755 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001756 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001757 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001758 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001759 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001760 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001761 }
1762
1763 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001764 Op->getType(), Name, InsertAtEnd);
1765}
1766
1767
1768// isConstantAllOnes - Helper function for several functions below
1769static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001770 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1771 return CI->isAllOnesValue();
1772 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1773 return CV->isAllOnesValue();
1774 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001775}
1776
Owen Andersonbb2501b2009-07-13 22:18:28 +00001777bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001778 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1779 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001780 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1781 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001782 return false;
1783}
1784
Owen Andersonbb2501b2009-07-13 22:18:28 +00001785bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001786 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1787 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001788 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001789 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001790 return false;
1791}
1792
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001793bool BinaryOperator::isNot(const Value *V) {
1794 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1795 return (Bop->getOpcode() == Instruction::Xor &&
1796 (isConstantAllOnes(Bop->getOperand(1)) ||
1797 isConstantAllOnes(Bop->getOperand(0))));
1798 return false;
1799}
1800
Chris Lattner2c7d1772005-04-24 07:28:37 +00001801Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001802 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001803}
1804
Chris Lattner2c7d1772005-04-24 07:28:37 +00001805const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1806 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001807}
1808
Dan Gohmana5b96452009-06-04 22:49:04 +00001809Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001810 return cast<BinaryOperator>(BinOp)->getOperand(1);
1811}
1812
1813const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1814 return getFNegArgument(const_cast<Value*>(BinOp));
1815}
1816
Chris Lattner2c7d1772005-04-24 07:28:37 +00001817Value *BinaryOperator::getNotArgument(Value *BinOp) {
1818 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1819 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1820 Value *Op0 = BO->getOperand(0);
1821 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001822 if (isConstantAllOnes(Op0)) return Op1;
1823
1824 assert(isConstantAllOnes(Op1));
1825 return Op0;
1826}
1827
Chris Lattner2c7d1772005-04-24 07:28:37 +00001828const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1829 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001830}
1831
1832
1833// swapOperands - Exchange the two operands to this instruction. This
1834// instruction is safe to use on any binary instruction and does not
1835// modify the semantics of the instruction. If the instruction is
1836// order dependent (SetLT f.e.) the opcode is changed.
1837//
1838bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001839 if (!isCommutative())
1840 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001841 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001842 return false;
1843}
1844
Dan Gohman1b849082009-09-07 23:54:19 +00001845void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1846 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1847}
1848
1849void BinaryOperator::setHasNoSignedWrap(bool b) {
1850 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1851}
1852
1853void BinaryOperator::setIsExact(bool b) {
1854 cast<SDivOperator>(this)->setIsExact(b);
1855}
1856
Nick Lewycky28a5f252009-09-27 21:33:04 +00001857bool BinaryOperator::hasNoUnsignedWrap() const {
1858 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1859}
1860
1861bool BinaryOperator::hasNoSignedWrap() const {
1862 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1863}
1864
1865bool BinaryOperator::isExact() const {
1866 return cast<SDivOperator>(this)->isExact();
1867}
1868
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001869//===----------------------------------------------------------------------===//
1870// CastInst Class
1871//===----------------------------------------------------------------------===//
1872
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001873// Just determine if this cast only deals with integral->integral conversion.
1874bool CastInst::isIntegerCast() const {
1875 switch (getOpcode()) {
1876 default: return false;
1877 case Instruction::ZExt:
1878 case Instruction::SExt:
1879 case Instruction::Trunc:
1880 return true;
1881 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00001882 return getOperand(0)->getType()->isIntegerTy() &&
1883 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001884 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001885}
1886
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001887bool CastInst::isLosslessCast() const {
1888 // Only BitCast can be lossless, exit fast if we're not BitCast
1889 if (getOpcode() != Instruction::BitCast)
1890 return false;
1891
1892 // Identity cast is always lossless
1893 const Type* SrcTy = getOperand(0)->getType();
1894 const Type* DstTy = getType();
1895 if (SrcTy == DstTy)
1896 return true;
1897
Reid Spencer8d9336d2006-12-31 05:26:44 +00001898 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00001899 if (SrcTy->isPointerTy())
1900 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001901 return false; // Other types have no identity values
1902}
1903
1904/// This function determines if the CastInst does not require any bits to be
1905/// changed in order to effect the cast. Essentially, it identifies cases where
1906/// no code gen is necessary for the cast, hence the name no-op cast. For
1907/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001908/// # bitcast i32* %x to i8*
1909/// # bitcast <2 x i32> %x to <4 x i16>
1910/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001911/// @brief Determine if the described cast is a no-op.
1912bool CastInst::isNoopCast(Instruction::CastOps Opcode,
1913 const Type *SrcTy,
1914 const Type *DestTy,
1915 const Type *IntPtrTy) {
1916 switch (Opcode) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001917 default:
1918 assert(!"Invalid CastOp");
1919 case Instruction::Trunc:
1920 case Instruction::ZExt:
1921 case Instruction::SExt:
1922 case Instruction::FPTrunc:
1923 case Instruction::FPExt:
1924 case Instruction::UIToFP:
1925 case Instruction::SIToFP:
1926 case Instruction::FPToUI:
1927 case Instruction::FPToSI:
1928 return false; // These always modify bits
1929 case Instruction::BitCast:
1930 return true; // BitCast never modifies bits.
1931 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001932 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001933 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001934 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001935 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001936 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001937 }
1938}
1939
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001940/// @brief Determine if a cast is a no-op.
1941bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1942 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
1943}
1944
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001945/// This function determines if a pair of casts can be eliminated and what
1946/// opcode should be used in the elimination. This assumes that there are two
1947/// instructions like this:
1948/// * %F = firstOpcode SrcTy %x to MidTy
1949/// * %S = secondOpcode MidTy %F to DstTy
1950/// The function returns a resultOpcode so these two casts can be replaced with:
1951/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1952/// If no such cast is permited, the function returns 0.
1953unsigned CastInst::isEliminableCastPair(
1954 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1955 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1956{
1957 // Define the 144 possibilities for these two cast instructions. The values
1958 // in this matrix determine what to do in a given situation and select the
1959 // case in the switch below. The rows correspond to firstOp, the columns
1960 // correspond to secondOp. In looking at the table below, keep in mind
1961 // the following cast properties:
1962 //
1963 // Size Compare Source Destination
1964 // Operator Src ? Size Type Sign Type Sign
1965 // -------- ------------ ------------------- ---------------------
1966 // TRUNC > Integer Any Integral Any
1967 // ZEXT < Integral Unsigned Integer Any
1968 // SEXT < Integral Signed Integer Any
1969 // FPTOUI n/a FloatPt n/a Integral Unsigned
1970 // FPTOSI n/a FloatPt n/a Integral Signed
1971 // UITOFP n/a Integral Unsigned FloatPt n/a
1972 // SITOFP n/a Integral Signed FloatPt n/a
1973 // FPTRUNC > FloatPt n/a FloatPt n/a
1974 // FPEXT < FloatPt n/a FloatPt n/a
1975 // PTRTOINT n/a Pointer n/a Integral Unsigned
1976 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmaneb7111b2010-04-07 23:22:42 +00001977 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001978 //
1979 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00001980 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1981 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00001982 // of the produced value (we no longer know the top-part is all zeros).
1983 // Further this conversion is often much more expensive for typical hardware,
1984 // and causes issues when building libgcc. We disallow fptosi+sext for the
1985 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001986 const unsigned numCastOps =
1987 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1988 static const uint8_t CastResults[numCastOps][numCastOps] = {
1989 // T F F U S F F P I B -+
1990 // R Z S P P I I T P 2 N T |
1991 // U E E 2 2 2 2 R E I T C +- secondOp
1992 // N X X U S F F N X N 2 V |
1993 // C T T I I P P C T T P T -+
1994 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1995 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1996 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001997 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1998 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001999 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2000 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2001 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2002 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2003 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2004 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2005 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2006 };
Chris Lattner25eea4d2010-07-12 01:19:22 +00002007
2008 // If either of the casts are a bitcast from scalar to vector, disallow the
2009 // merging.
2010 if ((firstOp == Instruction::BitCast &&
2011 isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2012 (secondOp == Instruction::BitCast &&
2013 isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2014 return 0; // Disallowed
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002015
2016 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2017 [secondOp-Instruction::CastOpsBegin];
2018 switch (ElimCase) {
2019 case 0:
2020 // categorically disallowed
2021 return 0;
2022 case 1:
2023 // allowed, use first cast's opcode
2024 return firstOp;
2025 case 2:
2026 // allowed, use second cast's opcode
2027 return secondOp;
2028 case 3:
2029 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002030 // is integer and we are not converting between a vector and a
Chris Lattner531732b2010-01-23 04:42:42 +00002031 // non vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002032 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002033 return firstOp;
2034 return 0;
2035 case 4:
2036 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002037 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002038 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002039 return firstOp;
2040 return 0;
2041 case 5:
2042 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002043 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002044 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002045 return secondOp;
2046 return 0;
2047 case 6:
2048 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002049 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002050 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002051 return secondOp;
2052 return 0;
2053 case 7: {
2054 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002055 if (!IntPtrTy)
2056 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002057 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2058 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002059 if (MidSize >= PtrSize)
2060 return Instruction::BitCast;
2061 return 0;
2062 }
2063 case 8: {
2064 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2065 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2066 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002067 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2068 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002069 if (SrcSize == DstSize)
2070 return Instruction::BitCast;
2071 else if (SrcSize < DstSize)
2072 return firstOp;
2073 return secondOp;
2074 }
2075 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2076 return Instruction::ZExt;
2077 case 10:
2078 // fpext followed by ftrunc is allowed if the bit size returned to is
2079 // the same as the original, in which case its just a bitcast
2080 if (SrcTy == DstTy)
2081 return Instruction::BitCast;
2082 return 0; // If the types are not the same we can't eliminate it.
2083 case 11:
2084 // bitcast followed by ptrtoint is allowed as long as the bitcast
2085 // is a pointer to pointer cast.
Duncan Sands19d0b472010-02-16 11:11:14 +00002086 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002087 return secondOp;
2088 return 0;
2089 case 12:
2090 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands19d0b472010-02-16 11:11:14 +00002091 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002092 return firstOp;
2093 return 0;
2094 case 13: {
2095 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002096 if (!IntPtrTy)
2097 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002098 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2099 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2100 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002101 if (SrcSize <= PtrSize && SrcSize == DstSize)
2102 return Instruction::BitCast;
2103 return 0;
2104 }
2105 case 99:
2106 // cast combination can't happen (error in input). This is for all cases
2107 // where the MidTy is not the same for the two cast instructions.
2108 assert(!"Invalid Cast Combination");
2109 return 0;
2110 default:
2111 assert(!"Error in CastResults table!!!");
2112 return 0;
2113 }
2114 return 0;
2115}
2116
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002117CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002118 const Twine &Name, Instruction *InsertBefore) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002119 // Construct and return the appropriate CastInst subclass
2120 switch (op) {
2121 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2122 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2123 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2124 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2125 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2126 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2127 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2128 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2129 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2130 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2131 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2132 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2133 default:
2134 assert(!"Invalid opcode provided");
2135 }
2136 return 0;
2137}
2138
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002139CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002140 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002141 // Construct and return the appropriate CastInst subclass
2142 switch (op) {
2143 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2144 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2145 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2146 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2147 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2148 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2149 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2150 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2151 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2152 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2153 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2154 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2155 default:
2156 assert(!"Invalid opcode provided");
2157 }
2158 return 0;
2159}
2160
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002161CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002162 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002163 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002164 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002165 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2166 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002167}
2168
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002169CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002170 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002171 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002172 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002173 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2174 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002175}
2176
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002177CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002178 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002179 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002180 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002181 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2182 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002183}
2184
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002185CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002186 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002187 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002188 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002189 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2190 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002191}
2192
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002193CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002194 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002195 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002196 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002197 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2198 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002199}
2200
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002201CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002202 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002203 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002204 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002205 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2206 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002207}
2208
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002209CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002210 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002211 BasicBlock *InsertAtEnd) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002212 assert(S->getType()->isPointerTy() && "Invalid cast");
2213 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002214 "Invalid cast");
2215
Duncan Sands9dff9be2010-02-15 16:12:20 +00002216 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002217 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2218 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002219}
2220
2221/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002222CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002223 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002224 Instruction *InsertBefore) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002225 assert(S->getType()->isPointerTy() && "Invalid cast");
2226 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002227 "Invalid cast");
2228
Duncan Sands9dff9be2010-02-15 16:12:20 +00002229 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002230 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2231 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002232}
2233
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002234CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002235 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002236 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002237 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002238 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002239 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2240 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002241 Instruction::CastOps opcode =
2242 (SrcBits == DstBits ? Instruction::BitCast :
2243 (SrcBits > DstBits ? Instruction::Trunc :
2244 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002245 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002246}
2247
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002248CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002249 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002250 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002251 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002252 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002253 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2254 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002255 Instruction::CastOps opcode =
2256 (SrcBits == DstBits ? Instruction::BitCast :
2257 (SrcBits > DstBits ? Instruction::Trunc :
2258 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002259 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002260}
2261
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002262CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002263 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002264 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002265 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002266 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002267 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2268 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002269 Instruction::CastOps opcode =
2270 (SrcBits == DstBits ? Instruction::BitCast :
2271 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002272 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002273}
2274
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002275CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002276 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002277 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002278 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002279 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002280 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2281 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002282 Instruction::CastOps opcode =
2283 (SrcBits == DstBits ? Instruction::BitCast :
2284 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002285 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002286}
2287
Duncan Sands55e50902008-01-06 10:12:28 +00002288// Check whether it is valid to call getCastOpcode for these types.
2289// This routine must be kept in sync with getCastOpcode.
2290bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2291 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2292 return false;
2293
2294 if (SrcTy == DestTy)
2295 return true;
2296
2297 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002298 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2299 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002300
2301 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002302 if (DestTy->isIntegerTy()) { // Casting to integral
2303 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002304 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002305 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002306 return true;
2307 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002308 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002309 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002310 } else { // Casting from something else
Duncan Sands19d0b472010-02-16 11:11:14 +00002311 return SrcTy->isPointerTy();
Duncan Sands55e50902008-01-06 10:12:28 +00002312 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002313 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2314 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002315 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002316 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002317 return true;
2318 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002319 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002320 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002321 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002322 return false;
2323 }
2324 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002325 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002326 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002327 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002328 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002329 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002330 return DestPTy->getBitWidth() == SrcBits;
2331 }
Duncan Sands19d0b472010-02-16 11:11:14 +00002332 } else if (DestTy->isPointerTy()) { // Casting to pointer
2333 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002334 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002335 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002336 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002337 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002338 return false;
2339 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002340 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002341 return false;
2342 }
2343}
2344
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002345// Provide a way to get a "cast" where the cast opcode is inferred from the
2346// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002347// logic in the castIsValid function below. This axiom should hold:
2348// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2349// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002350// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002351// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002352Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002353CastInst::getCastOpcode(
2354 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002355 // Get the bit sizes, we'll need these
2356 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002357 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2358 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002359
Duncan Sands55e50902008-01-06 10:12:28 +00002360 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2361 "Only first class types are castable!");
2362
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002363 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002364 if (DestTy->isIntegerTy()) { // Casting to integral
2365 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002366 if (DestBits < SrcBits)
2367 return Trunc; // int -> smaller int
2368 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002369 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002370 return SExt; // signed -> SEXT
2371 else
2372 return ZExt; // unsigned -> ZEXT
2373 } else {
2374 return BitCast; // Same size, No-op cast
2375 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002376 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002377 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002378 return FPToSI; // FP -> sint
2379 else
2380 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002381 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002382 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002383 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002384 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002385 return BitCast; // Same size, no-op cast
2386 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002387 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002388 "Casting from a value that is not first-class type");
2389 return PtrToInt; // ptr -> int
2390 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002391 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2392 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002393 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002394 return SIToFP; // sint -> FP
2395 else
2396 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002397 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002398 if (DestBits < SrcBits) {
2399 return FPTrunc; // FP -> smaller FP
2400 } else if (DestBits > SrcBits) {
2401 return FPExt; // FP -> larger FP
2402 } else {
2403 return BitCast; // same size, no-op cast
2404 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002405 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002406 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002407 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002408 PTy = NULL;
2409 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002410 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002411 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002412 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002413 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2414 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002415 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002416 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002417 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002418 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002419 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002420 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002421 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002422 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002423 }
Duncan Sands19d0b472010-02-16 11:11:14 +00002424 } else if (DestTy->isPointerTy()) {
2425 if (SrcTy->isPointerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002426 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002427 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002428 return IntToPtr; // int -> ptr
2429 } else {
2430 assert(!"Casting pointer to other than pointer or int");
2431 }
2432 } else {
2433 assert(!"Casting to type that is not first-class");
2434 }
2435
2436 // If we fall through to here we probably hit an assertion cast above
2437 // and assertions are not turned on. Anything we return is an error, so
2438 // BitCast is as good a choice as any.
2439 return BitCast;
2440}
2441
2442//===----------------------------------------------------------------------===//
2443// CastInst SubClass Constructors
2444//===----------------------------------------------------------------------===//
2445
2446/// Check that the construction parameters for a CastInst are correct. This
2447/// could be broken out into the separate constructors but it is useful to have
2448/// it in one place and to eliminate the redundant code for getting the sizes
2449/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002450bool
2451CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002452
2453 // Check for type sanity on the arguments
2454 const Type *SrcTy = S->getType();
Chris Lattner37bc78a2010-01-26 21:51:43 +00002455 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2456 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002457 return false;
2458
2459 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002460 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2461 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002462
2463 // Switch on the opcode provided
2464 switch (op) {
2465 default: return false; // This is an input error
2466 case Instruction::Trunc:
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::ZExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002470 return SrcTy->isIntOrIntVectorTy() &&
2471 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002472 case Instruction::SExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002473 return SrcTy->isIntOrIntVectorTy() &&
2474 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002475 case Instruction::FPTrunc:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002476 return SrcTy->isFPOrFPVectorTy() &&
2477 DstTy->isFPOrFPVectorTy() &&
Dan Gohman550c9af2008-08-14 20:04:46 +00002478 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002479 case Instruction::FPExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002480 return SrcTy->isFPOrFPVectorTy() &&
2481 DstTy->isFPOrFPVectorTy() &&
Dan Gohman550c9af2008-08-14 20:04:46 +00002482 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002483 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002484 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002485 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2486 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002487 return SVTy->getElementType()->isIntOrIntVectorTy() &&
2488 DVTy->getElementType()->isFPOrFPVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002489 SVTy->getNumElements() == DVTy->getNumElements();
2490 }
2491 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002492 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002493 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002494 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002495 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2496 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002497 return SVTy->getElementType()->isFPOrFPVectorTy() &&
2498 DVTy->getElementType()->isIntOrIntVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002499 SVTy->getNumElements() == DVTy->getNumElements();
2500 }
2501 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002502 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002503 case Instruction::PtrToInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00002504 return SrcTy->isPointerTy() && DstTy->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002505 case Instruction::IntToPtr:
Duncan Sands19d0b472010-02-16 11:11:14 +00002506 return SrcTy->isIntegerTy() && DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002507 case Instruction::BitCast:
2508 // BitCast implies a no-op cast of type only. No bits change.
2509 // However, you can't cast pointers to anything but pointers.
Duncan Sands19d0b472010-02-16 11:11:14 +00002510 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002511 return false;
2512
Duncan Sands55e50902008-01-06 10:12:28 +00002513 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002514 // these cases, the cast is okay if the source and destination bit widths
2515 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002516 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002517 }
2518}
2519
2520TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002521 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002522) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002523 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002524}
2525
2526TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002527 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002528) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002529 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
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, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002534) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002535 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002536}
2537
2538ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002539 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002540) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002541 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002542}
2543SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002544 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002545) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002546 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002547}
2548
Jeff Cohencc08c832006-12-02 02:22:01 +00002549SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002550 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002551) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002552 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
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, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002557) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002558 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002559}
2560
2561FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002562 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002563) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002564 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
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, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002569) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002570 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002571}
2572
2573FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002574 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002575) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002576 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
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, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002581) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002582 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002583}
2584
2585UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002586 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002587) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002588 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
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, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002593) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002594 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002595}
2596
2597SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002598 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002599) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002600 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
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, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002605) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002606 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002607}
2608
2609FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002610 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002611) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002612 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
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, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002617) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002618 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002619}
2620
2621FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002622 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002623) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002624 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
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, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002629) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002630 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002631}
2632
2633PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002634 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002635) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002636 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
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, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002641) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002642 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002643}
2644
2645IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002646 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002647) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002648 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
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, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002653) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002654 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002655}
2656
2657BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002658 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002659) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002660 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002661}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002662
2663//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002664// CmpInst Classes
2665//===----------------------------------------------------------------------===//
2666
Chris Lattneraec33da2010-01-22 06:25:37 +00002667void CmpInst::Anchor() const {}
2668
Nate Begemand2195702008-05-12 19:01:56 +00002669CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002670 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002671 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002672 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002673 OperandTraits<CmpInst>::op_begin(this),
2674 OperandTraits<CmpInst>::operands(this),
2675 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002676 Op<0>() = LHS;
2677 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002678 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002679 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002680}
Gabor Greiff6caff662008-05-10 08:32:32 +00002681
Nate Begemand2195702008-05-12 19:01:56 +00002682CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002683 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002684 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002685 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002686 OperandTraits<CmpInst>::op_begin(this),
2687 OperandTraits<CmpInst>::operands(this),
2688 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002689 Op<0>() = LHS;
2690 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002691 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002692 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002693}
2694
2695CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002696CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002697 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002698 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002699 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002700 if (InsertBefore)
2701 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2702 S1, S2, Name);
2703 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002704 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002705 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002706 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002707
2708 if (InsertBefore)
2709 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2710 S1, S2, Name);
2711 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002712 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002713 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002714}
2715
2716CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002717CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002718 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002719 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002720 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2721 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002722 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002723 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2724 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002725}
2726
2727void CmpInst::swapOperands() {
2728 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2729 IC->swapOperands();
2730 else
2731 cast<FCmpInst>(this)->swapOperands();
2732}
2733
2734bool CmpInst::isCommutative() {
2735 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2736 return IC->isCommutative();
2737 return cast<FCmpInst>(this)->isCommutative();
2738}
2739
2740bool CmpInst::isEquality() {
2741 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2742 return IC->isEquality();
2743 return cast<FCmpInst>(this)->isEquality();
2744}
2745
2746
Dan Gohman4e724382008-05-31 02:47:54 +00002747CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002748 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002749 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002750 case ICMP_EQ: return ICMP_NE;
2751 case ICMP_NE: return ICMP_EQ;
2752 case ICMP_UGT: return ICMP_ULE;
2753 case ICMP_ULT: return ICMP_UGE;
2754 case ICMP_UGE: return ICMP_ULT;
2755 case ICMP_ULE: return ICMP_UGT;
2756 case ICMP_SGT: return ICMP_SLE;
2757 case ICMP_SLT: return ICMP_SGE;
2758 case ICMP_SGE: return ICMP_SLT;
2759 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002760
Dan Gohman4e724382008-05-31 02:47:54 +00002761 case FCMP_OEQ: return FCMP_UNE;
2762 case FCMP_ONE: return FCMP_UEQ;
2763 case FCMP_OGT: return FCMP_ULE;
2764 case FCMP_OLT: return FCMP_UGE;
2765 case FCMP_OGE: return FCMP_ULT;
2766 case FCMP_OLE: return FCMP_UGT;
2767 case FCMP_UEQ: return FCMP_ONE;
2768 case FCMP_UNE: return FCMP_OEQ;
2769 case FCMP_UGT: return FCMP_OLE;
2770 case FCMP_ULT: return FCMP_OGE;
2771 case FCMP_UGE: return FCMP_OLT;
2772 case FCMP_ULE: return FCMP_OGT;
2773 case FCMP_ORD: return FCMP_UNO;
2774 case FCMP_UNO: return FCMP_ORD;
2775 case FCMP_TRUE: return FCMP_FALSE;
2776 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002777 }
2778}
2779
Reid Spencer266e42b2006-12-23 06:05:41 +00002780ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2781 switch (pred) {
2782 default: assert(! "Unknown icmp predicate!");
2783 case ICMP_EQ: case ICMP_NE:
2784 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2785 return pred;
2786 case ICMP_UGT: return ICMP_SGT;
2787 case ICMP_ULT: return ICMP_SLT;
2788 case ICMP_UGE: return ICMP_SGE;
2789 case ICMP_ULE: return ICMP_SLE;
2790 }
2791}
2792
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002793ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2794 switch (pred) {
2795 default: assert(! "Unknown icmp predicate!");
2796 case ICMP_EQ: case ICMP_NE:
2797 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2798 return pred;
2799 case ICMP_SGT: return ICMP_UGT;
2800 case ICMP_SLT: return ICMP_ULT;
2801 case ICMP_SGE: return ICMP_UGE;
2802 case ICMP_SLE: return ICMP_ULE;
2803 }
2804}
2805
Reid Spencer0286bc12007-02-28 22:00:54 +00002806/// Initialize a set of values that all satisfy the condition with C.
2807///
2808ConstantRange
2809ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2810 APInt Lower(C);
2811 APInt Upper(C);
2812 uint32_t BitWidth = C.getBitWidth();
2813 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002814 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002815 case ICmpInst::ICMP_EQ: Upper++; break;
2816 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00002817 case ICmpInst::ICMP_ULT:
2818 Lower = APInt::getMinValue(BitWidth);
2819 // Check for an empty-set condition.
2820 if (Lower == Upper)
2821 return ConstantRange(BitWidth, /*isFullSet=*/false);
2822 break;
2823 case ICmpInst::ICMP_SLT:
2824 Lower = APInt::getSignedMinValue(BitWidth);
2825 // Check for an empty-set condition.
2826 if (Lower == Upper)
2827 return ConstantRange(BitWidth, /*isFullSet=*/false);
2828 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00002829 case ICmpInst::ICMP_UGT:
2830 Lower++; Upper = APInt::getMinValue(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_SGT:
2836 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002837 // Check for an empty-set condition.
2838 if (Lower == Upper)
2839 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002840 break;
2841 case ICmpInst::ICMP_ULE:
2842 Lower = APInt::getMinValue(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_SLE:
2848 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
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_UGE:
2854 Upper = APInt::getMinValue(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 case ICmpInst::ICMP_SGE:
2860 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002861 // Check for a full-set condition.
2862 if (Lower == Upper)
2863 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002864 break;
2865 }
2866 return ConstantRange(Lower, Upper);
2867}
2868
Dan Gohman4e724382008-05-31 02:47:54 +00002869CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002870 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002871 default: assert(!"Unknown cmp predicate!");
2872 case ICMP_EQ: case ICMP_NE:
2873 return pred;
2874 case ICMP_SGT: return ICMP_SLT;
2875 case ICMP_SLT: return ICMP_SGT;
2876 case ICMP_SGE: return ICMP_SLE;
2877 case ICMP_SLE: return ICMP_SGE;
2878 case ICMP_UGT: return ICMP_ULT;
2879 case ICMP_ULT: return ICMP_UGT;
2880 case ICMP_UGE: return ICMP_ULE;
2881 case ICMP_ULE: return ICMP_UGE;
2882
Reid Spencerd9436b62006-11-20 01:22:35 +00002883 case FCMP_FALSE: case FCMP_TRUE:
2884 case FCMP_OEQ: case FCMP_ONE:
2885 case FCMP_UEQ: case FCMP_UNE:
2886 case FCMP_ORD: case FCMP_UNO:
2887 return pred;
2888 case FCMP_OGT: return FCMP_OLT;
2889 case FCMP_OLT: return FCMP_OGT;
2890 case FCMP_OGE: return FCMP_OLE;
2891 case FCMP_OLE: return FCMP_OGE;
2892 case FCMP_UGT: return FCMP_ULT;
2893 case FCMP_ULT: return FCMP_UGT;
2894 case FCMP_UGE: return FCMP_ULE;
2895 case FCMP_ULE: return FCMP_UGE;
2896 }
2897}
2898
Reid Spencer266e42b2006-12-23 06:05:41 +00002899bool CmpInst::isUnsigned(unsigned short predicate) {
2900 switch (predicate) {
2901 default: return false;
2902 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2903 case ICmpInst::ICMP_UGE: return true;
2904 }
2905}
2906
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002907bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002908 switch (predicate) {
2909 default: return false;
2910 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2911 case ICmpInst::ICMP_SGE: return true;
2912 }
2913}
2914
2915bool CmpInst::isOrdered(unsigned short predicate) {
2916 switch (predicate) {
2917 default: return false;
2918 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2919 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2920 case FCmpInst::FCMP_ORD: return true;
2921 }
2922}
2923
2924bool CmpInst::isUnordered(unsigned short predicate) {
2925 switch (predicate) {
2926 default: return false;
2927 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2928 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2929 case FCmpInst::FCMP_UNO: return true;
2930 }
2931}
2932
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002933bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
2934 switch(predicate) {
2935 default: return false;
2936 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
2937 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
2938 }
2939}
2940
2941bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
2942 switch(predicate) {
2943 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
2944 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
2945 default: return false;
2946 }
2947}
2948
2949
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002950//===----------------------------------------------------------------------===//
2951// SwitchInst Implementation
2952//===----------------------------------------------------------------------===//
2953
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002954void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002955 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002956 ReservedSpace = 2+NumCases*2;
2957 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002958 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002959
Gabor Greif2d3024d2008-05-26 21:33:52 +00002960 OperandList[0] = Value;
2961 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002962}
2963
Chris Lattner2195fc42007-02-24 00:55:48 +00002964/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2965/// switch on and a default destination. The number of additional cases can
2966/// be specified here to make memory allocation more efficient. This
2967/// constructor can also autoinsert before another instruction.
2968SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2969 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00002970 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2971 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002972 init(Value, Default, NumCases);
2973}
2974
2975/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2976/// switch on and a default destination. The number of additional cases can
2977/// be specified here to make memory allocation more efficient. This
2978/// constructor also autoinserts at the end of the specified BasicBlock.
2979SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2980 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00002981 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2982 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002983 init(Value, Default, NumCases);
2984}
2985
Misha Brukmanb1c93172005-04-21 23:48:37 +00002986SwitchInst::SwitchInst(const SwitchInst &SI)
Owen Anderson55f1c092009-08-13 21:58:54 +00002987 : TerminatorInst(Type::getVoidTy(SI.getContext()), Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002988 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002989 Use *OL = OperandList, *InOL = SI.OperandList;
2990 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002991 OL[i] = InOL[i];
2992 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002993 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002994 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002995}
2996
Gordon Henriksen14a55692007-12-10 02:14:30 +00002997SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002998 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002999}
3000
3001
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003002/// addCase - Add an entry to the switch instruction...
3003///
Chris Lattner47ac1872005-02-24 05:32:09 +00003004void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003005 unsigned OpNo = NumOperands;
3006 if (OpNo+2 > ReservedSpace)
3007 resizeOperands(0); // Get more space!
3008 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003009 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003010 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00003011 OperandList[OpNo] = OnVal;
3012 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003013}
3014
3015/// removeCase - This method removes the specified successor from the switch
3016/// instruction. Note that this cannot be used to remove the default
3017/// destination (successor #0).
3018///
3019void SwitchInst::removeCase(unsigned idx) {
3020 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003021 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
3022
3023 unsigned NumOps = getNumOperands();
3024 Use *OL = OperandList;
3025
3026 // Move everything after this operand down.
3027 //
3028 // FIXME: we could just swap with the end of the list, then erase. However,
3029 // client might not expect this to happen. The code as it is thrashes the
3030 // use/def lists, which is kinda lame.
3031 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
3032 OL[i-2] = OL[i];
3033 OL[i-2+1] = OL[i+1];
3034 }
3035
3036 // Nuke the last value.
3037 OL[NumOps-2].set(0);
3038 OL[NumOps-2+1].set(0);
3039 NumOperands = NumOps-2;
3040}
3041
3042/// resizeOperands - resize operands - This adjusts the length of the operands
3043/// list according to the following behavior:
3044/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00003045/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003046/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3047/// 3. If NumOps == NumOperands, trim the reserved space.
3048///
3049void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003050 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003051 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003052 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003053 } else if (NumOps*2 > NumOperands) {
3054 // No resize needed.
3055 if (ReservedSpace >= NumOps) return;
3056 } else if (NumOps == NumOperands) {
3057 if (ReservedSpace == NumOps) return;
3058 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003059 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003060 }
3061
3062 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003063 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003064 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003065 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003066 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003067 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003068 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003069 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003070}
3071
3072
3073BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3074 return getSuccessor(idx);
3075}
3076unsigned SwitchInst::getNumSuccessorsV() const {
3077 return getNumSuccessors();
3078}
3079void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3080 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003081}
Chris Lattnerf22be932004-10-15 23:52:53 +00003082
Chris Lattner3ed871f2009-10-27 19:13:16 +00003083//===----------------------------------------------------------------------===//
3084// SwitchInst Implementation
3085//===----------------------------------------------------------------------===//
3086
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003087void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003088 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003089 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003090 ReservedSpace = 1+NumDests;
3091 NumOperands = 1;
3092 OperandList = allocHungoffUses(ReservedSpace);
3093
3094 OperandList[0] = Address;
3095}
3096
3097
3098/// resizeOperands - resize operands - This adjusts the length of the operands
3099/// list according to the following behavior:
3100/// 1. If NumOps == 0, grow the operand list in response to a push_back style
3101/// of operation. This grows the number of ops by 2 times.
3102/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3103/// 3. If NumOps == NumOperands, trim the reserved space.
3104///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003105void IndirectBrInst::resizeOperands(unsigned NumOps) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003106 unsigned e = getNumOperands();
3107 if (NumOps == 0) {
3108 NumOps = e*2;
3109 } else if (NumOps*2 > NumOperands) {
3110 // No resize needed.
3111 if (ReservedSpace >= NumOps) return;
3112 } else if (NumOps == NumOperands) {
3113 if (ReservedSpace == NumOps) return;
3114 } else {
3115 return;
3116 }
3117
3118 ReservedSpace = NumOps;
3119 Use *NewOps = allocHungoffUses(NumOps);
3120 Use *OldOps = OperandList;
3121 for (unsigned i = 0; i != e; ++i)
3122 NewOps[i] = OldOps[i];
3123 OperandList = NewOps;
3124 if (OldOps) Use::zap(OldOps, OldOps + e, true);
3125}
3126
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003127IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3128 Instruction *InsertBefore)
3129: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003130 0, 0, InsertBefore) {
3131 init(Address, NumCases);
3132}
3133
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003134IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3135 BasicBlock *InsertAtEnd)
3136: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003137 0, 0, InsertAtEnd) {
3138 init(Address, NumCases);
3139}
3140
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003141IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3142 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003143 allocHungoffUses(IBI.getNumOperands()),
3144 IBI.getNumOperands()) {
3145 Use *OL = OperandList, *InOL = IBI.OperandList;
3146 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3147 OL[i] = InOL[i];
3148 SubclassOptionalData = IBI.SubclassOptionalData;
3149}
3150
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003151IndirectBrInst::~IndirectBrInst() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003152 dropHungoffUses(OperandList);
3153}
3154
3155/// addDestination - Add a destination.
3156///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003157void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003158 unsigned OpNo = NumOperands;
3159 if (OpNo+1 > ReservedSpace)
3160 resizeOperands(0); // Get more space!
3161 // Initialize some new operands.
3162 assert(OpNo < ReservedSpace && "Growing didn't work!");
3163 NumOperands = OpNo+1;
3164 OperandList[OpNo] = DestBB;
3165}
3166
3167/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003168/// indirectbr instruction.
3169void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003170 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3171
3172 unsigned NumOps = getNumOperands();
3173 Use *OL = OperandList;
3174
3175 // Replace this value with the last one.
3176 OL[idx+1] = OL[NumOps-1];
3177
3178 // Nuke the last value.
3179 OL[NumOps-1].set(0);
3180 NumOperands = NumOps-1;
3181}
3182
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003183BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003184 return getSuccessor(idx);
3185}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003186unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003187 return getNumSuccessors();
3188}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003189void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003190 setSuccessor(idx, B);
3191}
3192
3193//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003194// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003195//===----------------------------------------------------------------------===//
3196
Chris Lattnerf22be932004-10-15 23:52:53 +00003197// Define these methods here so vtables don't get emitted into every translation
3198// unit that uses these classes.
3199
Devang Patel11cf3f42009-10-27 22:16:29 +00003200GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3201 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003202}
3203
Devang Patel11cf3f42009-10-27 22:16:29 +00003204BinaryOperator *BinaryOperator::clone_impl() const {
3205 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003206}
3207
Devang Patel11cf3f42009-10-27 22:16:29 +00003208FCmpInst* FCmpInst::clone_impl() const {
3209 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003210}
3211
Devang Patel11cf3f42009-10-27 22:16:29 +00003212ICmpInst* ICmpInst::clone_impl() const {
3213 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003214}
3215
Devang Patel11cf3f42009-10-27 22:16:29 +00003216ExtractValueInst *ExtractValueInst::clone_impl() const {
3217 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003218}
3219
Devang Patel11cf3f42009-10-27 22:16:29 +00003220InsertValueInst *InsertValueInst::clone_impl() const {
3221 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003222}
3223
Devang Patel11cf3f42009-10-27 22:16:29 +00003224AllocaInst *AllocaInst::clone_impl() const {
3225 return new AllocaInst(getAllocatedType(),
3226 (Value*)getOperand(0),
3227 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003228}
3229
Devang Patel11cf3f42009-10-27 22:16:29 +00003230LoadInst *LoadInst::clone_impl() const {
3231 return new LoadInst(getOperand(0),
3232 Twine(), isVolatile(),
3233 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003234}
3235
Devang Patel11cf3f42009-10-27 22:16:29 +00003236StoreInst *StoreInst::clone_impl() const {
3237 return new StoreInst(getOperand(0), getOperand(1),
3238 isVolatile(), getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003239}
3240
Devang Patel11cf3f42009-10-27 22:16:29 +00003241TruncInst *TruncInst::clone_impl() const {
3242 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003243}
3244
Devang Patel11cf3f42009-10-27 22:16:29 +00003245ZExtInst *ZExtInst::clone_impl() const {
3246 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003247}
3248
Devang Patel11cf3f42009-10-27 22:16:29 +00003249SExtInst *SExtInst::clone_impl() const {
3250 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003251}
3252
Devang Patel11cf3f42009-10-27 22:16:29 +00003253FPTruncInst *FPTruncInst::clone_impl() const {
3254 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003255}
3256
Devang Patel11cf3f42009-10-27 22:16:29 +00003257FPExtInst *FPExtInst::clone_impl() const {
3258 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003259}
3260
Devang Patel11cf3f42009-10-27 22:16:29 +00003261UIToFPInst *UIToFPInst::clone_impl() const {
3262 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003263}
3264
Devang Patel11cf3f42009-10-27 22:16:29 +00003265SIToFPInst *SIToFPInst::clone_impl() const {
3266 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003267}
3268
Devang Patel11cf3f42009-10-27 22:16:29 +00003269FPToUIInst *FPToUIInst::clone_impl() const {
3270 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003271}
3272
Devang Patel11cf3f42009-10-27 22:16:29 +00003273FPToSIInst *FPToSIInst::clone_impl() const {
3274 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003275}
3276
Devang Patel11cf3f42009-10-27 22:16:29 +00003277PtrToIntInst *PtrToIntInst::clone_impl() const {
3278 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003279}
3280
Devang Patel11cf3f42009-10-27 22:16:29 +00003281IntToPtrInst *IntToPtrInst::clone_impl() const {
3282 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003283}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003284
Devang Patel11cf3f42009-10-27 22:16:29 +00003285BitCastInst *BitCastInst::clone_impl() const {
3286 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003287}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003288
Devang Patel11cf3f42009-10-27 22:16:29 +00003289CallInst *CallInst::clone_impl() const {
3290 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003291}
3292
Devang Patel11cf3f42009-10-27 22:16:29 +00003293SelectInst *SelectInst::clone_impl() const {
3294 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003295}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003296
Devang Patel11cf3f42009-10-27 22:16:29 +00003297VAArgInst *VAArgInst::clone_impl() const {
3298 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003299}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003300
Devang Patel11cf3f42009-10-27 22:16:29 +00003301ExtractElementInst *ExtractElementInst::clone_impl() const {
3302 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003303}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003304
Devang Patel11cf3f42009-10-27 22:16:29 +00003305InsertElementInst *InsertElementInst::clone_impl() const {
3306 return InsertElementInst::Create(getOperand(0),
3307 getOperand(1),
3308 getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003309}
3310
Devang Patel11cf3f42009-10-27 22:16:29 +00003311ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3312 return new ShuffleVectorInst(getOperand(0),
3313 getOperand(1),
3314 getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003315}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003316
Devang Patel11cf3f42009-10-27 22:16:29 +00003317PHINode *PHINode::clone_impl() const {
3318 return new PHINode(*this);
3319}
3320
3321ReturnInst *ReturnInst::clone_impl() const {
3322 return new(getNumOperands()) ReturnInst(*this);
3323}
3324
3325BranchInst *BranchInst::clone_impl() const {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00003326 unsigned Ops(getNumOperands());
Devang Patel11cf3f42009-10-27 22:16:29 +00003327 return new(Ops, Ops == 1) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003328}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003329
Devang Patel11cf3f42009-10-27 22:16:29 +00003330SwitchInst *SwitchInst::clone_impl() const {
3331 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003332}
3333
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003334IndirectBrInst *IndirectBrInst::clone_impl() const {
3335 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003336}
3337
3338
Devang Patel11cf3f42009-10-27 22:16:29 +00003339InvokeInst *InvokeInst::clone_impl() const {
3340 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003341}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003342
Devang Patel11cf3f42009-10-27 22:16:29 +00003343UnwindInst *UnwindInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003344 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003345 return new UnwindInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003346}
3347
Devang Patel11cf3f42009-10-27 22:16:29 +00003348UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003349 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003350 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003351}