blob: 098560a549a6b95c867a34a9bcd87786c6a45ba2 [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
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000015#include "llvm/Constants.h"
16#include "llvm/DerivedTypes.h"
17#include "llvm/Function.h"
18#include "llvm/Instructions.h"
19#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000020#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000021#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000022using namespace llvm;
23
Chris Lattner3e13b8c2008-01-02 23:42:30 +000024//===----------------------------------------------------------------------===//
25// CallSite Class
26//===----------------------------------------------------------------------===//
27
Duncan Sands85fab3a2008-02-18 17:32:13 +000028CallSite::CallSite(Instruction *C) {
29 assert((isa<CallInst>(C) || isa<InvokeInst>(C)) && "Not a call!");
30 I = C;
31}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000032unsigned CallSite::getCallingConv() const {
33 if (CallInst *CI = dyn_cast<CallInst>(I))
34 return CI->getCallingConv();
35 else
36 return cast<InvokeInst>(I)->getCallingConv();
37}
38void CallSite::setCallingConv(unsigned CC) {
39 if (CallInst *CI = dyn_cast<CallInst>(I))
40 CI->setCallingConv(CC);
41 else
42 cast<InvokeInst>(I)->setCallingConv(CC);
43}
Chris Lattner8a923e72008-03-12 17:45:29 +000044const PAListPtr &CallSite::getParamAttrs() const {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000045 if (CallInst *CI = dyn_cast<CallInst>(I))
46 return CI->getParamAttrs();
47 else
48 return cast<InvokeInst>(I)->getParamAttrs();
49}
Chris Lattner8a923e72008-03-12 17:45:29 +000050void CallSite::setParamAttrs(const PAListPtr &PAL) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000051 if (CallInst *CI = dyn_cast<CallInst>(I))
52 CI->setParamAttrs(PAL);
53 else
54 cast<InvokeInst>(I)->setParamAttrs(PAL);
55}
Dale Johannesen89268bc2008-02-19 21:38:47 +000056bool CallSite::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
Duncan Sands5208d1a2007-11-28 17:07:01 +000057 if (CallInst *CI = dyn_cast<CallInst>(I))
Dale Johannesen89268bc2008-02-19 21:38:47 +000058 return CI->paramHasAttr(i, attr);
Duncan Sands5208d1a2007-11-28 17:07:01 +000059 else
Dale Johannesen89268bc2008-02-19 21:38:47 +000060 return cast<InvokeInst>(I)->paramHasAttr(i, attr);
Duncan Sands5208d1a2007-11-28 17:07:01 +000061}
Dale Johanneseneabc5f32008-02-22 17:49:45 +000062uint16_t CallSite::getParamAlignment(uint16_t i) const {
63 if (CallInst *CI = dyn_cast<CallInst>(I))
64 return CI->getParamAlignment(i);
65 else
66 return cast<InvokeInst>(I)->getParamAlignment(i);
67}
68
Duncan Sands38ef3a82007-12-03 20:06:50 +000069bool CallSite::doesNotAccessMemory() const {
70 if (CallInst *CI = dyn_cast<CallInst>(I))
71 return CI->doesNotAccessMemory();
72 else
73 return cast<InvokeInst>(I)->doesNotAccessMemory();
74}
Duncan Sands78c88722008-07-08 08:38:44 +000075void CallSite::setDoesNotAccessMemory(bool doesNotAccessMemory) {
76 if (CallInst *CI = dyn_cast<CallInst>(I))
77 CI->setDoesNotAccessMemory(doesNotAccessMemory);
78 else
79 cast<InvokeInst>(I)->setDoesNotAccessMemory(doesNotAccessMemory);
80}
Duncan Sands38ef3a82007-12-03 20:06:50 +000081bool CallSite::onlyReadsMemory() const {
82 if (CallInst *CI = dyn_cast<CallInst>(I))
83 return CI->onlyReadsMemory();
84 else
85 return cast<InvokeInst>(I)->onlyReadsMemory();
86}
Duncan Sands78c88722008-07-08 08:38:44 +000087void CallSite::setOnlyReadsMemory(bool onlyReadsMemory) {
88 if (CallInst *CI = dyn_cast<CallInst>(I))
89 CI->setOnlyReadsMemory(onlyReadsMemory);
90 else
91 cast<InvokeInst>(I)->setOnlyReadsMemory(onlyReadsMemory);
92}
93bool CallSite::doesNotReturn() const {
94 if (CallInst *CI = dyn_cast<CallInst>(I))
95 return CI->doesNotReturn();
96 else
97 return cast<InvokeInst>(I)->doesNotReturn();
98}
99void CallSite::setDoesNotReturn(bool doesNotReturn) {
100 if (CallInst *CI = dyn_cast<CallInst>(I))
101 CI->setDoesNotReturn(doesNotReturn);
102 else
103 cast<InvokeInst>(I)->setDoesNotReturn(doesNotReturn);
104}
Duncan Sands3353ed02007-12-18 09:59:50 +0000105bool CallSite::doesNotThrow() const {
Duncan Sands8e4847e2007-12-16 15:51:49 +0000106 if (CallInst *CI = dyn_cast<CallInst>(I))
Duncan Sands3353ed02007-12-18 09:59:50 +0000107 return CI->doesNotThrow();
Duncan Sands8e4847e2007-12-16 15:51:49 +0000108 else
Duncan Sands3353ed02007-12-18 09:59:50 +0000109 return cast<InvokeInst>(I)->doesNotThrow();
Duncan Sands8e4847e2007-12-16 15:51:49 +0000110}
Duncan Sandsaa31b922007-12-19 21:13:37 +0000111void CallSite::setDoesNotThrow(bool doesNotThrow) {
112 if (CallInst *CI = dyn_cast<CallInst>(I))
113 CI->setDoesNotThrow(doesNotThrow);
114 else
115 cast<InvokeInst>(I)->setDoesNotThrow(doesNotThrow);
116}
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000117
Matthijs Kooijmanb0dffd62008-06-05 08:04:58 +0000118bool CallSite::hasArgument(const Value *Arg) const {
Matthijs Kooijmand901e582008-06-04 16:31:12 +0000119 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E; ++AI)
120 if (AI->get() == Arg)
121 return true;
122 return false;
123}
124
Gordon Henriksen14a55692007-12-10 02:14:30 +0000125//===----------------------------------------------------------------------===//
126// TerminatorInst Class
127//===----------------------------------------------------------------------===//
128
129// Out of line virtual method, so the vtable, etc has a home.
130TerminatorInst::~TerminatorInst() {
131}
132
Gabor Greiff6caff662008-05-10 08:32:32 +0000133//===----------------------------------------------------------------------===//
134// UnaryInstruction Class
135//===----------------------------------------------------------------------===//
136
Gordon Henriksen14a55692007-12-10 02:14:30 +0000137// Out of line virtual method, so the vtable, etc has a home.
138UnaryInstruction::~UnaryInstruction() {
139}
140
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000141//===----------------------------------------------------------------------===//
142// PHINode Class
143//===----------------------------------------------------------------------===//
144
145PHINode::PHINode(const PHINode &PN)
146 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +0000147 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000148 ReservedSpace(PN.getNumOperands()) {
149 Use *OL = OperandList;
150 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000151 OL[i] = PN.getOperand(i);
152 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000153 }
154}
155
Gordon Henriksen14a55692007-12-10 02:14:30 +0000156PHINode::~PHINode() {
Chris Lattner7d6aac82008-06-16 04:02:40 +0000157 if (OperandList)
158 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000159}
160
161// removeIncomingValue - Remove an incoming value. This is useful if a
162// predecessor basic block is deleted.
163Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
164 unsigned NumOps = getNumOperands();
165 Use *OL = OperandList;
166 assert(Idx*2 < NumOps && "BB not in PHI node!");
167 Value *Removed = OL[Idx*2];
168
169 // Move everything after this operand down.
170 //
171 // FIXME: we could just swap with the end of the list, then erase. However,
172 // client might not expect this to happen. The code as it is thrashes the
173 // use/def lists, which is kinda lame.
174 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
175 OL[i-2] = OL[i];
176 OL[i-2+1] = OL[i+1];
177 }
178
179 // Nuke the last value.
180 OL[NumOps-2].set(0);
181 OL[NumOps-2+1].set(0);
182 NumOperands = NumOps-2;
183
184 // If the PHI node is dead, because it has zero entries, nuke it now.
185 if (NumOps == 2 && DeletePHIIfEmpty) {
186 // If anyone is using this PHI, make them use a dummy value instead...
187 replaceAllUsesWith(UndefValue::get(getType()));
188 eraseFromParent();
189 }
190 return Removed;
191}
192
193/// resizeOperands - resize operands - This adjusts the length of the operands
194/// list according to the following behavior:
195/// 1. If NumOps == 0, grow the operand list in response to a push_back style
196/// of operation. This grows the number of ops by 1.5 times.
197/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
198/// 3. If NumOps == NumOperands, trim the reserved space.
199///
200void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000201 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000202 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000203 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000204 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
205 } else if (NumOps*2 > NumOperands) {
206 // No resize needed.
207 if (ReservedSpace >= NumOps) return;
208 } else if (NumOps == NumOperands) {
209 if (ReservedSpace == NumOps) return;
210 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000211 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000212 }
213
214 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000215 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000216 Use *NewOps = allocHungoffUses(NumOps);
Dan Gohman031f0bb2008-06-23 16:45:24 +0000217 std::copy(OldOps, OldOps + e, NewOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000218 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000219 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000220}
221
Nate Begemanb3923212005-08-04 23:24:19 +0000222/// hasConstantValue - If the specified PHI node always merges together the same
223/// value, return the value, otherwise return null.
224///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000225Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000226 // If the PHI node only has one incoming value, eliminate the PHI node...
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000227 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000228 if (getIncomingValue(0) != this) // not X = phi X
229 return getIncomingValue(0);
230 else
231 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000232 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000233
Nate Begemanb3923212005-08-04 23:24:19 +0000234 // Otherwise if all of the incoming values are the same for the PHI, replace
235 // the PHI node with the incoming value.
236 //
237 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000238 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000239 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000240 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000241 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000242 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000243 if (InVal && getIncomingValue(i) != InVal)
244 return 0; // Not the same, bail out.
245 else
246 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000247 }
Nate Begemanb3923212005-08-04 23:24:19 +0000248
249 // The only case that could cause InVal to be null is if we have a PHI node
250 // that only has entries for itself. In this case, there is no entry into the
251 // loop, so kill the PHI.
252 //
253 if (InVal == 0) InVal = UndefValue::get(getType());
254
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000255 // If we have a PHI node like phi(X, undef, X), where X is defined by some
256 // instruction, we cannot always return X as the result of the PHI node. Only
257 // do this if X is not an instruction (thus it must dominate the PHI block),
258 // or if the client is prepared to deal with this possibility.
259 if (HasUndefInput && !AllowNonDominatingInstruction)
260 if (Instruction *IV = dyn_cast<Instruction>(InVal))
261 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000262 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000263 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000264 return 0; // Cannot guarantee that InVal dominates this PHINode.
265
Nate Begemanb3923212005-08-04 23:24:19 +0000266 // All of the incoming values are the same, return the value now.
267 return InVal;
268}
269
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000270
271//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000272// CallInst Implementation
273//===----------------------------------------------------------------------===//
274
Gordon Henriksen14a55692007-12-10 02:14:30 +0000275CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000276}
277
Chris Lattner054ba2c2007-02-13 00:58:44 +0000278void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000279 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
280 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000281 OL[0] = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000282
Misha Brukmanb1c93172005-04-21 23:48:37 +0000283 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000284 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000285 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000286
Chris Lattner054ba2c2007-02-13 00:58:44 +0000287 assert((NumParams == FTy->getNumParams() ||
288 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000289 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000290 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000291 assert((i >= FTy->getNumParams() ||
292 FTy->getParamType(i) == Params[i]->getType()) &&
293 "Calling a function with a bad signature!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000294 OL[i+1] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000295 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000296}
297
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000298void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000299 assert(NumOperands == 3 && "NumOperands not set up?");
300 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000301 OL[0] = Func;
302 OL[1] = Actual1;
303 OL[2] = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000304
305 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000306 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000307 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000308
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000309 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000310 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000311 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000312 assert((0 >= FTy->getNumParams() ||
313 FTy->getParamType(0) == Actual1->getType()) &&
314 "Calling a function with a bad signature!");
315 assert((1 >= FTy->getNumParams() ||
316 FTy->getParamType(1) == Actual2->getType()) &&
317 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000318}
319
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000320void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000321 assert(NumOperands == 2 && "NumOperands not set up?");
322 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000323 OL[0] = Func;
324 OL[1] = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000325
326 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000327 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000328 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000329
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000330 assert((FTy->getNumParams() == 1 ||
331 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000332 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000333 assert((0 == FTy->getNumParams() ||
334 FTy->getParamType(0) == Actual->getType()) &&
335 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000336}
337
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000338void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000339 assert(NumOperands == 1 && "NumOperands not set up?");
340 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000341 OL[0] = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000342
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000343 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000344 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000345 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000346
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000347 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000348}
349
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000350CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000351 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000352 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
353 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000354 Instruction::Call,
355 OperandTraits<CallInst>::op_end(this) - 2,
356 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000357 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000358 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000359}
360
361CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
362 BasicBlock *InsertAtEnd)
363 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
364 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000365 Instruction::Call,
366 OperandTraits<CallInst>::op_end(this) - 2,
367 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000368 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000369 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000370}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000371CallInst::CallInst(Value *Func, const std::string &Name,
372 Instruction *InsertBefore)
373 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
374 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000375 Instruction::Call,
376 OperandTraits<CallInst>::op_end(this) - 1,
377 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000378 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000379 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000380}
381
382CallInst::CallInst(Value *Func, const std::string &Name,
383 BasicBlock *InsertAtEnd)
384 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
385 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000386 Instruction::Call,
387 OperandTraits<CallInst>::op_end(this) - 1,
388 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000389 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000390 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000391}
392
Misha Brukmanb1c93172005-04-21 23:48:37 +0000393CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000394 : Instruction(CI.getType(), Instruction::Call,
395 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000396 CI.getNumOperands()) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000397 setParamAttrs(CI.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000398 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000399 Use *OL = OperandList;
400 Use *InOL = CI.OperandList;
401 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000402 OL[i] = InOL[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000403}
404
Eric Christopher901b1a72008-05-16 20:39:43 +0000405void CallInst::addParamAttr(unsigned i, ParameterAttributes attr) {
406 PAListPtr PAL = getParamAttrs();
407 PAL = PAL.addAttr(i, attr);
408 setParamAttrs(PAL);
409}
410
Duncan Sands78c88722008-07-08 08:38:44 +0000411void CallInst::removeParamAttr(unsigned i, ParameterAttributes attr) {
412 PAListPtr PAL = getParamAttrs();
413 PAL = PAL.removeAttr(i, attr);
414 setParamAttrs(PAL);
415}
416
Chris Lattnerc994c022008-03-13 05:00:21 +0000417bool CallInst::paramHasAttr(unsigned i, ParameterAttributes attr) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000418 if (ParamAttrs.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000419 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000420 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000421 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000422 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000423}
424
Duncan Sands5208d1a2007-11-28 17:07:01 +0000425
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000426//===----------------------------------------------------------------------===//
427// InvokeInst Implementation
428//===----------------------------------------------------------------------===//
429
430void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000431 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000432 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
433 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000434 OL[0] = Fn;
435 OL[1] = IfNormal;
436 OL[2] = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000437 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000438 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000439 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000440
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000441 assert(((NumArgs == FTy->getNumParams()) ||
442 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000443 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000444
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000445 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000446 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000447 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000448 "Invoking a function with a bad signature!");
449
Gabor Greif2d3024d2008-05-26 21:33:52 +0000450 OL[i+3] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000451 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000452}
453
Misha Brukmanb1c93172005-04-21 23:48:37 +0000454InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000455 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000456 OperandTraits<InvokeInst>::op_end(this)
457 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000458 II.getNumOperands()) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000459 setParamAttrs(II.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000460 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000461 Use *OL = OperandList, *InOL = II.OperandList;
462 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000463 OL[i] = InOL[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000464}
465
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000466BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
467 return getSuccessor(idx);
468}
469unsigned InvokeInst::getNumSuccessorsV() const {
470 return getNumSuccessors();
471}
472void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
473 return setSuccessor(idx, B);
474}
475
Chris Lattnerc994c022008-03-13 05:00:21 +0000476bool InvokeInst::paramHasAttr(unsigned i, ParameterAttributes attr) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000477 if (ParamAttrs.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000478 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000479 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000480 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000481 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000482}
483
Eric Christopher901b1a72008-05-16 20:39:43 +0000484void InvokeInst::addParamAttr(unsigned i, ParameterAttributes attr) {
485 PAListPtr PAL = getParamAttrs();
486 PAL = PAL.addAttr(i, attr);
487 setParamAttrs(PAL);
488}
489
Duncan Sands78c88722008-07-08 08:38:44 +0000490void InvokeInst::removeParamAttr(unsigned i, ParameterAttributes attr) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000491 PAListPtr PAL = getParamAttrs();
Duncan Sands78c88722008-07-08 08:38:44 +0000492 PAL = PAL.removeAttr(i, attr);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000493 setParamAttrs(PAL);
494}
495
Duncan Sands5208d1a2007-11-28 17:07:01 +0000496
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000497//===----------------------------------------------------------------------===//
498// ReturnInst Implementation
499//===----------------------------------------------------------------------===//
500
Chris Lattner2195fc42007-02-24 00:55:48 +0000501ReturnInst::ReturnInst(const ReturnInst &RI)
502 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000503 OperandTraits<ReturnInst>::op_end(this) -
504 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000505 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000506 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000507 Op<0>() = RI.Op<0>();
Chris Lattner2195fc42007-02-24 00:55:48 +0000508}
509
510ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000511 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000512 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
513 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000514 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000515 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000516}
517ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000518 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000519 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
520 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000521 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000522 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000523}
524ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000525 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000526 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000527}
528
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000529unsigned ReturnInst::getNumSuccessorsV() const {
530 return getNumSuccessors();
531}
532
Devang Patelae682fb2008-02-26 17:56:20 +0000533/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
534/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000535void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000536 assert(0 && "ReturnInst has no successors!");
537}
538
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000539BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
540 assert(0 && "ReturnInst has no successors!");
541 abort();
542 return 0;
543}
544
Devang Patel59643e52008-02-23 00:35:18 +0000545ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000546}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000547
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000548//===----------------------------------------------------------------------===//
549// UnwindInst Implementation
550//===----------------------------------------------------------------------===//
551
Chris Lattner2195fc42007-02-24 00:55:48 +0000552UnwindInst::UnwindInst(Instruction *InsertBefore)
553 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
554}
555UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
556 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
557}
558
559
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000560unsigned UnwindInst::getNumSuccessorsV() const {
561 return getNumSuccessors();
562}
563
564void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000565 assert(0 && "UnwindInst has no successors!");
566}
567
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000568BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
569 assert(0 && "UnwindInst has no successors!");
570 abort();
571 return 0;
572}
573
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000574//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000575// UnreachableInst Implementation
576//===----------------------------------------------------------------------===//
577
Chris Lattner2195fc42007-02-24 00:55:48 +0000578UnreachableInst::UnreachableInst(Instruction *InsertBefore)
579 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
580}
581UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
582 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
583}
584
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000585unsigned UnreachableInst::getNumSuccessorsV() const {
586 return getNumSuccessors();
587}
588
589void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
590 assert(0 && "UnwindInst has no successors!");
591}
592
593BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
594 assert(0 && "UnwindInst has no successors!");
595 abort();
596 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000597}
598
599//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000600// BranchInst Implementation
601//===----------------------------------------------------------------------===//
602
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000603void BranchInst::AssertOK() {
604 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000605 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000606 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000607}
608
Chris Lattner2195fc42007-02-24 00:55:48 +0000609BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000610 : TerminatorInst(Type::VoidTy, Instruction::Br,
611 OperandTraits<BranchInst>::op_end(this) - 1,
612 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000613 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000614 Op<0>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000615}
616BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
617 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000618 : TerminatorInst(Type::VoidTy, Instruction::Br,
619 OperandTraits<BranchInst>::op_end(this) - 3,
620 3, InsertBefore) {
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000621 Op<0>() = IfTrue;
622 Op<1>() = IfFalse;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000623 Op<2>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000624#ifndef NDEBUG
625 AssertOK();
626#endif
627}
628
629BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000630 : TerminatorInst(Type::VoidTy, Instruction::Br,
631 OperandTraits<BranchInst>::op_end(this) - 1,
632 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000633 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000634 Op<0>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000635}
636
637BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
638 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000639 : TerminatorInst(Type::VoidTy, Instruction::Br,
640 OperandTraits<BranchInst>::op_end(this) - 3,
641 3, InsertAtEnd) {
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000642 Op<0>() = IfTrue;
643 Op<1>() = IfFalse;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000644 Op<2>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000645#ifndef NDEBUG
646 AssertOK();
647#endif
648}
649
650
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000651BranchInst::BranchInst(const BranchInst &BI) :
Gabor Greiff6caff662008-05-10 08:32:32 +0000652 TerminatorInst(Type::VoidTy, Instruction::Br,
653 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
654 BI.getNumOperands()) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000655 OperandList[0] = BI.getOperand(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000656 if (BI.getNumOperands() != 1) {
657 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000658 OperandList[1] = BI.getOperand(1);
659 OperandList[2] = BI.getOperand(2);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000660 }
661}
662
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000663BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
664 return getSuccessor(idx);
665}
666unsigned BranchInst::getNumSuccessorsV() const {
667 return getNumSuccessors();
668}
669void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
670 setSuccessor(idx, B);
671}
672
673
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000674//===----------------------------------------------------------------------===//
675// AllocationInst Implementation
676//===----------------------------------------------------------------------===//
677
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000678static Value *getAISize(Value *Amt) {
679 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000680 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000681 else {
682 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000683 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000684 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000685 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000686 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000687 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000688}
689
Misha Brukmanb1c93172005-04-21 23:48:37 +0000690AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000691 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000692 Instruction *InsertBefore)
Christopher Lambedf07882007-12-17 01:12:55 +0000693 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000694 InsertBefore) {
695 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000696 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000697 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000698}
699
Misha Brukmanb1c93172005-04-21 23:48:37 +0000700AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000701 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000702 BasicBlock *InsertAtEnd)
Christopher Lambedf07882007-12-17 01:12:55 +0000703 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000704 InsertAtEnd) {
705 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000706 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000707 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000708}
709
Gordon Henriksen14a55692007-12-10 02:14:30 +0000710// Out of line virtual method, so the vtable, etc has a home.
711AllocationInst::~AllocationInst() {
712}
713
Dan Gohmanaa583d72008-03-24 16:55:58 +0000714void AllocationInst::setAlignment(unsigned Align) {
715 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
716 SubclassData = Log2_32(Align) + 1;
717 assert(getAlignment() == Align && "Alignment representation error!");
718}
719
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000720bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000721 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
722 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000723 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000724}
725
726const Type *AllocationInst::getAllocatedType() const {
727 return getType()->getElementType();
728}
729
730AllocaInst::AllocaInst(const AllocaInst &AI)
731 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000732 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000733}
734
735MallocInst::MallocInst(const MallocInst &MI)
736 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000737 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000738}
739
740//===----------------------------------------------------------------------===//
741// FreeInst Implementation
742//===----------------------------------------------------------------------===//
743
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000744void FreeInst::AssertOK() {
745 assert(isa<PointerType>(getOperand(0)->getType()) &&
746 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000747}
748
749FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000750 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000751 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000752}
753
754FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000755 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000756 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000757}
758
759
760//===----------------------------------------------------------------------===//
761// LoadInst Implementation
762//===----------------------------------------------------------------------===//
763
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000764void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000765 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000766 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000767}
768
769LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000770 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000771 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000772 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000773 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000774 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000775 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000776}
777
778LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000779 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000780 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000781 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000782 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000783 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000784 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000785}
786
787LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
788 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000789 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000790 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000791 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000792 setAlignment(0);
793 AssertOK();
794 setName(Name);
795}
796
797LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
798 unsigned Align, Instruction *InsertBef)
799 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
800 Load, Ptr, InsertBef) {
801 setVolatile(isVolatile);
802 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000803 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000804 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000805}
806
Dan Gohman68659282007-07-18 20:51:11 +0000807LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
808 unsigned Align, BasicBlock *InsertAE)
809 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
810 Load, Ptr, InsertAE) {
811 setVolatile(isVolatile);
812 setAlignment(Align);
813 AssertOK();
814 setName(Name);
815}
816
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000817LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
818 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000819 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000820 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000821 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000822 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000823 AssertOK();
824 setName(Name);
825}
826
827
828
829LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000830 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
831 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000832 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000833 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000834 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000835 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000836}
837
838LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000839 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
840 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000841 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000842 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000843 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000844 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000845}
846
847LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
848 Instruction *InsertBef)
849: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000850 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000851 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000852 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000853 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000854 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000855}
856
857LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
858 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000859 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
860 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000861 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000862 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000863 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000864 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000865}
866
Christopher Lamb84485702007-04-22 19:24:39 +0000867void LoadInst::setAlignment(unsigned Align) {
868 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
869 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
870}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000871
872//===----------------------------------------------------------------------===//
873// StoreInst Implementation
874//===----------------------------------------------------------------------===//
875
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000876void StoreInst::AssertOK() {
877 assert(isa<PointerType>(getOperand(1)->getType()) &&
878 "Ptr must have pointer type!");
879 assert(getOperand(0)->getType() ==
880 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000881 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000882}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000883
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000884
885StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000886 : Instruction(Type::VoidTy, Store,
887 OperandTraits<StoreInst>::op_begin(this),
888 OperandTraits<StoreInst>::operands(this),
889 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000890 Op<0>() = val;
891 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000892 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000893 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000894 AssertOK();
895}
896
897StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000898 : Instruction(Type::VoidTy, Store,
899 OperandTraits<StoreInst>::op_begin(this),
900 OperandTraits<StoreInst>::operands(this),
901 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000902 Op<0>() = val;
903 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000904 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000905 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000906 AssertOK();
907}
908
Misha Brukmanb1c93172005-04-21 23:48:37 +0000909StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000910 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000911 : Instruction(Type::VoidTy, Store,
912 OperandTraits<StoreInst>::op_begin(this),
913 OperandTraits<StoreInst>::operands(this),
914 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000915 Op<0>() = val;
916 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000917 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000918 setAlignment(0);
919 AssertOK();
920}
921
922StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
923 unsigned Align, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000924 : Instruction(Type::VoidTy, Store,
925 OperandTraits<StoreInst>::op_begin(this),
926 OperandTraits<StoreInst>::operands(this),
927 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000928 Op<0>() = val;
929 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +0000930 setVolatile(isVolatile);
931 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000932 AssertOK();
933}
934
Misha Brukmanb1c93172005-04-21 23:48:37 +0000935StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000936 unsigned Align, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000937 : Instruction(Type::VoidTy, Store,
938 OperandTraits<StoreInst>::op_begin(this),
939 OperandTraits<StoreInst>::operands(this),
940 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000941 Op<0>() = val;
942 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +0000943 setVolatile(isVolatile);
944 setAlignment(Align);
945 AssertOK();
946}
947
948StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000949 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000950 : Instruction(Type::VoidTy, Store,
951 OperandTraits<StoreInst>::op_begin(this),
952 OperandTraits<StoreInst>::operands(this),
953 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000954 Op<0>() = val;
955 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000956 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000957 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000958 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000959}
960
Christopher Lamb84485702007-04-22 19:24:39 +0000961void StoreInst::setAlignment(unsigned Align) {
962 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
963 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
964}
965
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000966//===----------------------------------------------------------------------===//
967// GetElementPtrInst Implementation
968//===----------------------------------------------------------------------===//
969
Christopher Lambedf07882007-12-17 01:12:55 +0000970static unsigned retrieveAddrSpace(const Value *Val) {
971 return cast<PointerType>(Val->getType())->getAddressSpace();
972}
973
Gabor Greif21ba1842008-06-06 20:28:12 +0000974void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Nate Begeman8a4afd92008-07-25 17:24:13 +0000975 const std::string &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000976 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
977 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000978 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000979
Chris Lattner79807c3d2007-01-31 19:47:18 +0000980 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000981 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +0000982
983 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000984}
985
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +0000986void GetElementPtrInst::init(Value *Ptr, Value *Idx, const std::string &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000987 assert(NumOperands == 2 && "NumOperands not initialized?");
988 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000989 OL[0] = Ptr;
990 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +0000991
992 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +0000993}
994
Gabor Greiff6caff662008-05-10 08:32:32 +0000995GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +0000996 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +0000997 OperandTraits<GetElementPtrInst>::op_end(this)
998 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000999 GEPI.getNumOperands()) {
1000 Use *OL = OperandList;
1001 Use *GEPIOL = GEPI.OperandList;
1002 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001003 OL[i] = GEPIOL[i];
Gabor Greiff6caff662008-05-10 08:32:32 +00001004}
1005
Chris Lattner82981202005-05-03 05:43:30 +00001006GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1007 const std::string &Name, Instruction *InBe)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001008 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001009 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001010 GetElementPtr,
1011 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1012 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001013 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001014}
1015
1016GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1017 const std::string &Name, BasicBlock *IAE)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001018 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001019 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001020 GetElementPtr,
1021 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1022 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001023 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001024}
1025
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001026// getIndexedType - Returns the type of the element that would be loaded with
1027// a load instruction with the specified parameters.
1028//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001029// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001030// pointer type.
1031//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001032const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +00001033 Value* const *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001034 unsigned NumIdx) {
1035 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1036 if (!PTy) return 0; // Type isn't a pointer type!
1037 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001038
1039 // Handle the special case of the empty set index set...
Dan Gohman12fce772008-05-15 19:50:34 +00001040 if (NumIdx == 0)
1041 return Agg;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001042
Dan Gohman1ecaf452008-05-31 00:58:22 +00001043 unsigned CurIdx = 1;
1044 for (; CurIdx != NumIdx; ++CurIdx) {
1045 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1046 if (!CT || isa<PointerType>(CT)) return 0;
1047 Value *Index = Idxs[CurIdx];
1048 if (!CT->indexValid(Index)) return 0;
1049 Agg = CT->getTypeAtIndex(Index);
1050
1051 // If the new type forwards to another type, then it is in the middle
1052 // of being refined to another type (and hence, may have dropped all
1053 // references to what it was using before). So, use the new forwarded
1054 // type.
1055 if (const Type *Ty = Agg->getForwardedType())
1056 Agg = Ty;
1057 }
1058 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001059}
1060
Chris Lattner82981202005-05-03 05:43:30 +00001061const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1062 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1063 if (!PTy) return 0; // Type isn't a pointer type!
1064
1065 // Check the pointer index.
1066 if (!PTy->indexValid(Idx)) return 0;
1067
Chris Lattnerc2233332005-05-03 16:44:45 +00001068 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001069}
1070
Chris Lattner45f15572007-04-14 00:12:57 +00001071
1072/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1073/// zeros. If so, the result pointer and the first operand have the same
1074/// value, just potentially different types.
1075bool GetElementPtrInst::hasAllZeroIndices() const {
1076 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1077 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1078 if (!CI->isZero()) return false;
1079 } else {
1080 return false;
1081 }
1082 }
1083 return true;
1084}
1085
Chris Lattner27058292007-04-27 20:35:56 +00001086/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1087/// constant integers. If so, the result pointer and the first operand have
1088/// a constant offset between them.
1089bool GetElementPtrInst::hasAllConstantIndices() const {
1090 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1091 if (!isa<ConstantInt>(getOperand(i)))
1092 return false;
1093 }
1094 return true;
1095}
1096
Chris Lattner45f15572007-04-14 00:12:57 +00001097
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001098//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001099// ExtractElementInst Implementation
1100//===----------------------------------------------------------------------===//
1101
1102ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001103 const std::string &Name,
1104 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001105 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001106 ExtractElement,
1107 OperandTraits<ExtractElementInst>::op_begin(this),
1108 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001109 assert(isValidOperands(Val, Index) &&
1110 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001111 Op<0>() = Val;
1112 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001113 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001114}
1115
Chris Lattner65511ff2006-10-05 06:24:58 +00001116ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1117 const std::string &Name,
1118 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001119 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001120 ExtractElement,
1121 OperandTraits<ExtractElementInst>::op_begin(this),
1122 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001123 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001124 assert(isValidOperands(Val, Index) &&
1125 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001126 Op<0>() = Val;
1127 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001128 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001129}
1130
1131
Robert Bocchino23004482006-01-10 19:05:34 +00001132ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001133 const std::string &Name,
1134 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001135 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001136 ExtractElement,
1137 OperandTraits<ExtractElementInst>::op_begin(this),
1138 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001139 assert(isValidOperands(Val, Index) &&
1140 "Invalid extractelement instruction operands!");
1141
Gabor Greif2d3024d2008-05-26 21:33:52 +00001142 Op<0>() = Val;
1143 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001144 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001145}
1146
Chris Lattner65511ff2006-10-05 06:24:58 +00001147ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1148 const std::string &Name,
1149 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001150 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001151 ExtractElement,
1152 OperandTraits<ExtractElementInst>::op_begin(this),
1153 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001154 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001155 assert(isValidOperands(Val, Index) &&
1156 "Invalid extractelement instruction operands!");
1157
Gabor Greif2d3024d2008-05-26 21:33:52 +00001158 Op<0>() = Val;
1159 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001160 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001161}
1162
1163
Chris Lattner54865b32006-04-08 04:05:48 +00001164bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001165 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001166 return false;
1167 return true;
1168}
1169
1170
Robert Bocchino23004482006-01-10 19:05:34 +00001171//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001172// InsertElementInst Implementation
1173//===----------------------------------------------------------------------===//
1174
Chris Lattner0875d942006-04-14 22:20:32 +00001175InsertElementInst::InsertElementInst(const InsertElementInst &IE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001176 : Instruction(IE.getType(), InsertElement,
1177 OperandTraits<InsertElementInst>::op_begin(this), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001178 Op<0>() = IE.Op<0>();
1179 Op<1>() = IE.Op<1>();
1180 Op<2>() = IE.Op<2>();
Chris Lattner0875d942006-04-14 22:20:32 +00001181}
Chris Lattner54865b32006-04-08 04:05:48 +00001182InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001183 const std::string &Name,
1184 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001185 : Instruction(Vec->getType(), InsertElement,
1186 OperandTraits<InsertElementInst>::op_begin(this),
1187 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001188 assert(isValidOperands(Vec, Elt, Index) &&
1189 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001190 Op<0>() = Vec;
1191 Op<1>() = Elt;
1192 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001193 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001194}
1195
Chris Lattner65511ff2006-10-05 06:24:58 +00001196InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1197 const std::string &Name,
1198 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001199 : Instruction(Vec->getType(), InsertElement,
1200 OperandTraits<InsertElementInst>::op_begin(this),
1201 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001202 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001203 assert(isValidOperands(Vec, Elt, Index) &&
1204 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001205 Op<0>() = Vec;
1206 Op<1>() = Elt;
1207 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001208 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001209}
1210
1211
Chris Lattner54865b32006-04-08 04:05:48 +00001212InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001213 const std::string &Name,
1214 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001215 : Instruction(Vec->getType(), InsertElement,
1216 OperandTraits<InsertElementInst>::op_begin(this),
1217 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001218 assert(isValidOperands(Vec, Elt, Index) &&
1219 "Invalid insertelement instruction operands!");
1220
Gabor Greif2d3024d2008-05-26 21:33:52 +00001221 Op<0>() = Vec;
1222 Op<1>() = Elt;
1223 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001224 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001225}
1226
Chris Lattner65511ff2006-10-05 06:24:58 +00001227InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1228 const std::string &Name,
1229 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001230: Instruction(Vec->getType(), InsertElement,
1231 OperandTraits<InsertElementInst>::op_begin(this),
1232 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001233 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001234 assert(isValidOperands(Vec, Elt, Index) &&
1235 "Invalid insertelement instruction operands!");
1236
Gabor Greif2d3024d2008-05-26 21:33:52 +00001237 Op<0>() = Vec;
1238 Op<1>() = Elt;
1239 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001240 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001241}
1242
Chris Lattner54865b32006-04-08 04:05:48 +00001243bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1244 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001245 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001246 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001247
Reid Spencerd84d35b2007-02-15 02:26:10 +00001248 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001249 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001250
Reid Spencer8d9336d2006-12-31 05:26:44 +00001251 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001252 return false; // Third operand of insertelement must be uint.
1253 return true;
1254}
1255
1256
Robert Bocchinoca27f032006-01-17 20:07:22 +00001257//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001258// ShuffleVectorInst Implementation
1259//===----------------------------------------------------------------------===//
1260
Chris Lattner0875d942006-04-14 22:20:32 +00001261ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
Gabor Greiff6caff662008-05-10 08:32:32 +00001262 : Instruction(SV.getType(), ShuffleVector,
1263 OperandTraits<ShuffleVectorInst>::op_begin(this),
1264 OperandTraits<ShuffleVectorInst>::operands(this)) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001265 Op<0>() = SV.Op<0>();
1266 Op<1>() = SV.Op<1>();
1267 Op<2>() = SV.Op<2>();
Chris Lattner0875d942006-04-14 22:20:32 +00001268}
1269
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001270ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1271 const std::string &Name,
1272 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +00001273 : Instruction(V1->getType(), ShuffleVector,
1274 OperandTraits<ShuffleVectorInst>::op_begin(this),
1275 OperandTraits<ShuffleVectorInst>::operands(this),
1276 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001277 assert(isValidOperands(V1, V2, Mask) &&
1278 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001279 Op<0>() = V1;
1280 Op<1>() = V2;
1281 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001282 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001283}
1284
1285ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1286 const std::string &Name,
1287 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00001288 : Instruction(V1->getType(), ShuffleVector,
1289 OperandTraits<ShuffleVectorInst>::op_begin(this),
1290 OperandTraits<ShuffleVectorInst>::operands(this),
1291 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001292 assert(isValidOperands(V1, V2, Mask) &&
1293 "Invalid shuffle vector instruction operands!");
1294
Gabor Greif2d3024d2008-05-26 21:33:52 +00001295 Op<0>() = V1;
1296 Op<1>() = V2;
1297 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001298 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001299}
1300
1301bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1302 const Value *Mask) {
Chris Lattnerf724e342008-03-02 05:28:33 +00001303 if (!isa<VectorType>(V1->getType()) ||
1304 V1->getType() != V2->getType())
1305 return false;
1306
1307 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1308 if (!isa<Constant>(Mask) || MaskTy == 0 ||
1309 MaskTy->getElementType() != Type::Int32Ty ||
1310 MaskTy->getNumElements() !=
1311 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001312 return false;
1313 return true;
1314}
1315
Chris Lattnerf724e342008-03-02 05:28:33 +00001316/// getMaskValue - Return the index from the shuffle mask for the specified
1317/// output result. This is either -1 if the element is undef or a number less
1318/// than 2*numelements.
1319int ShuffleVectorInst::getMaskValue(unsigned i) const {
1320 const Constant *Mask = cast<Constant>(getOperand(2));
1321 if (isa<UndefValue>(Mask)) return -1;
1322 if (isa<ConstantAggregateZero>(Mask)) return 0;
1323 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1324 assert(i < MaskCV->getNumOperands() && "Index out of range");
1325
1326 if (isa<UndefValue>(MaskCV->getOperand(i)))
1327 return -1;
1328 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1329}
1330
Dan Gohman12fce772008-05-15 19:50:34 +00001331//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001332// InsertValueInst Class
1333//===----------------------------------------------------------------------===//
1334
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001335void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
1336 unsigned NumIdx, const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001337 assert(NumOperands == 2 && "NumOperands not initialized?");
1338 Op<0>() = Agg;
1339 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001340
Dan Gohman1ecaf452008-05-31 00:58:22 +00001341 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001342 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001343}
1344
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001345void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
1346 const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001347 assert(NumOperands == 2 && "NumOperands not initialized?");
1348 Op<0>() = Agg;
1349 Op<1>() = Val;
1350
1351 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001352 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001353}
1354
1355InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001356 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001357 OperandTraits<InsertValueInst>::op_begin(this), 2),
1358 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001359 Op<0>() = IVI.getOperand(0);
1360 Op<1>() = IVI.getOperand(1);
Dan Gohman0752bff2008-05-23 00:36:11 +00001361}
1362
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001363InsertValueInst::InsertValueInst(Value *Agg,
1364 Value *Val,
1365 unsigned Idx,
1366 const std::string &Name,
1367 Instruction *InsertBefore)
1368 : Instruction(Agg->getType(), InsertValue,
1369 OperandTraits<InsertValueInst>::op_begin(this),
1370 2, InsertBefore) {
1371 init(Agg, Val, Idx, Name);
1372}
1373
1374InsertValueInst::InsertValueInst(Value *Agg,
1375 Value *Val,
1376 unsigned Idx,
1377 const std::string &Name,
1378 BasicBlock *InsertAtEnd)
1379 : Instruction(Agg->getType(), InsertValue,
1380 OperandTraits<InsertValueInst>::op_begin(this),
1381 2, InsertAtEnd) {
1382 init(Agg, Val, Idx, Name);
1383}
1384
Dan Gohman0752bff2008-05-23 00:36:11 +00001385//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001386// ExtractValueInst Class
1387//===----------------------------------------------------------------------===//
1388
Gabor Greifcbcc4952008-06-06 21:06:32 +00001389void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Gabor Greif21ba1842008-06-06 20:28:12 +00001390 const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001391 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001392
Dan Gohman1ecaf452008-05-31 00:58:22 +00001393 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001394 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001395}
1396
Gabor Greifcbcc4952008-06-06 21:06:32 +00001397void ExtractValueInst::init(unsigned Idx, const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001398 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001399
1400 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001401 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001402}
1403
1404ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001405 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001406 Indices(EVI.Indices) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001407}
1408
Dan Gohman12fce772008-05-15 19:50:34 +00001409// getIndexedType - Returns the type of the element that would be extracted
1410// with an extractvalue instruction with the specified parameters.
1411//
1412// A null type is returned if the indices are invalid for the specified
1413// pointer type.
1414//
1415const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001416 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001417 unsigned NumIdx) {
1418 unsigned CurIdx = 0;
1419 for (; CurIdx != NumIdx; ++CurIdx) {
1420 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001421 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1422 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001423 if (!CT->indexValid(Index)) return 0;
1424 Agg = CT->getTypeAtIndex(Index);
1425
1426 // If the new type forwards to another type, then it is in the middle
1427 // of being refined to another type (and hence, may have dropped all
1428 // references to what it was using before). So, use the new forwarded
1429 // type.
1430 if (const Type *Ty = Agg->getForwardedType())
1431 Agg = Ty;
1432 }
1433 return CurIdx == NumIdx ? Agg : 0;
1434}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001435
Dan Gohman4a3125b2008-06-17 21:07:55 +00001436const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001437 unsigned Idx) {
1438 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001439}
1440
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001441//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001442// BinaryOperator Class
1443//===----------------------------------------------------------------------===//
1444
Chris Lattner2195fc42007-02-24 00:55:48 +00001445BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1446 const Type *Ty, const std::string &Name,
1447 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +00001448 : Instruction(Ty, iType,
1449 OperandTraits<BinaryOperator>::op_begin(this),
1450 OperandTraits<BinaryOperator>::operands(this),
1451 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001452 Op<0>() = S1;
1453 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00001454 init(iType);
1455 setName(Name);
1456}
1457
1458BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1459 const Type *Ty, const std::string &Name,
1460 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00001461 : Instruction(Ty, iType,
1462 OperandTraits<BinaryOperator>::op_begin(this),
1463 OperandTraits<BinaryOperator>::operands(this),
1464 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001465 Op<0>() = S1;
1466 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00001467 init(iType);
1468 setName(Name);
1469}
1470
1471
1472void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001473 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001474 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001475 assert(LHS->getType() == RHS->getType() &&
1476 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001477#ifndef NDEBUG
1478 switch (iType) {
1479 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001480 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001481 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001482 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001483 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001484 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001485 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001486 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001487 case UDiv:
1488 case SDiv:
1489 assert(getType() == LHS->getType() &&
1490 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001491 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1492 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001493 "Incorrect operand type (not integer) for S/UDIV");
1494 break;
1495 case FDiv:
1496 assert(getType() == LHS->getType() &&
1497 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001498 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1499 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001500 && "Incorrect operand type (not floating point) for FDIV");
1501 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001502 case URem:
1503 case SRem:
1504 assert(getType() == LHS->getType() &&
1505 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001506 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1507 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001508 "Incorrect operand type (not integer) for S/UREM");
1509 break;
1510 case FRem:
1511 assert(getType() == LHS->getType() &&
1512 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001513 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1514 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001515 && "Incorrect operand type (not floating point) for FREM");
1516 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001517 case Shl:
1518 case LShr:
1519 case AShr:
1520 assert(getType() == LHS->getType() &&
1521 "Shift operation should return same type as operands!");
1522 assert(getType()->isInteger() &&
1523 "Shift operation requires integer operands");
1524 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001525 case And: case Or:
1526 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001527 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001528 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001529 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001530 (isa<VectorType>(getType()) &&
1531 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001532 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001533 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001534 default:
1535 break;
1536 }
1537#endif
1538}
1539
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001540BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001541 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001542 Instruction *InsertBefore) {
1543 assert(S1->getType() == S2->getType() &&
1544 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001545 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001546}
1547
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001548BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001549 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001550 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001551 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001552 InsertAtEnd->getInstList().push_back(Res);
1553 return Res;
1554}
1555
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001556BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001557 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001558 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1559 return new BinaryOperator(Instruction::Sub,
1560 zero, Op,
1561 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001562}
1563
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001564BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001565 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001566 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1567 return new BinaryOperator(Instruction::Sub,
1568 zero, Op,
1569 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001570}
1571
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001572BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001573 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001574 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001575 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001576 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001577 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001578 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001579 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001580 }
1581
1582 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001583 Op->getType(), Name, InsertBefore);
1584}
1585
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001586BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001587 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001588 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001589 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001590 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001591 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001592 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001593 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001594 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001595 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001596 }
1597
1598 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001599 Op->getType(), Name, InsertAtEnd);
1600}
1601
1602
1603// isConstantAllOnes - Helper function for several functions below
1604static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001605 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1606 return CI->isAllOnesValue();
1607 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1608 return CV->isAllOnesValue();
1609 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001610}
1611
1612bool BinaryOperator::isNeg(const Value *V) {
1613 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1614 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001615 return Bop->getOperand(0) ==
1616 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001617 return false;
1618}
1619
1620bool BinaryOperator::isNot(const Value *V) {
1621 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1622 return (Bop->getOpcode() == Instruction::Xor &&
1623 (isConstantAllOnes(Bop->getOperand(1)) ||
1624 isConstantAllOnes(Bop->getOperand(0))));
1625 return false;
1626}
1627
Chris Lattner2c7d1772005-04-24 07:28:37 +00001628Value *BinaryOperator::getNegArgument(Value *BinOp) {
1629 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1630 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001631}
1632
Chris Lattner2c7d1772005-04-24 07:28:37 +00001633const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1634 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001635}
1636
Chris Lattner2c7d1772005-04-24 07:28:37 +00001637Value *BinaryOperator::getNotArgument(Value *BinOp) {
1638 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1639 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1640 Value *Op0 = BO->getOperand(0);
1641 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001642 if (isConstantAllOnes(Op0)) return Op1;
1643
1644 assert(isConstantAllOnes(Op1));
1645 return Op0;
1646}
1647
Chris Lattner2c7d1772005-04-24 07:28:37 +00001648const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1649 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001650}
1651
1652
1653// swapOperands - Exchange the two operands to this instruction. This
1654// instruction is safe to use on any binary instruction and does not
1655// modify the semantics of the instruction. If the instruction is
1656// order dependent (SetLT f.e.) the opcode is changed.
1657//
1658bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001659 if (!isCommutative())
1660 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001661 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001662 return false;
1663}
1664
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001665//===----------------------------------------------------------------------===//
1666// CastInst Class
1667//===----------------------------------------------------------------------===//
1668
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001669// Just determine if this cast only deals with integral->integral conversion.
1670bool CastInst::isIntegerCast() const {
1671 switch (getOpcode()) {
1672 default: return false;
1673 case Instruction::ZExt:
1674 case Instruction::SExt:
1675 case Instruction::Trunc:
1676 return true;
1677 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001678 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001679 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001680}
1681
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001682bool CastInst::isLosslessCast() const {
1683 // Only BitCast can be lossless, exit fast if we're not BitCast
1684 if (getOpcode() != Instruction::BitCast)
1685 return false;
1686
1687 // Identity cast is always lossless
1688 const Type* SrcTy = getOperand(0)->getType();
1689 const Type* DstTy = getType();
1690 if (SrcTy == DstTy)
1691 return true;
1692
Reid Spencer8d9336d2006-12-31 05:26:44 +00001693 // Pointer to pointer is always lossless.
1694 if (isa<PointerType>(SrcTy))
1695 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001696 return false; // Other types have no identity values
1697}
1698
1699/// This function determines if the CastInst does not require any bits to be
1700/// changed in order to effect the cast. Essentially, it identifies cases where
1701/// no code gen is necessary for the cast, hence the name no-op cast. For
1702/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001703/// # bitcast i32* %x to i8*
1704/// # bitcast <2 x i32> %x to <4 x i16>
1705/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001706/// @brief Determine if a cast is a no-op.
1707bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1708 switch (getOpcode()) {
1709 default:
1710 assert(!"Invalid CastOp");
1711 case Instruction::Trunc:
1712 case Instruction::ZExt:
1713 case Instruction::SExt:
1714 case Instruction::FPTrunc:
1715 case Instruction::FPExt:
1716 case Instruction::UIToFP:
1717 case Instruction::SIToFP:
1718 case Instruction::FPToUI:
1719 case Instruction::FPToSI:
1720 return false; // These always modify bits
1721 case Instruction::BitCast:
1722 return true; // BitCast never modifies bits.
1723 case Instruction::PtrToInt:
1724 return IntPtrTy->getPrimitiveSizeInBits() ==
1725 getType()->getPrimitiveSizeInBits();
1726 case Instruction::IntToPtr:
1727 return IntPtrTy->getPrimitiveSizeInBits() ==
1728 getOperand(0)->getType()->getPrimitiveSizeInBits();
1729 }
1730}
1731
1732/// This function determines if a pair of casts can be eliminated and what
1733/// opcode should be used in the elimination. This assumes that there are two
1734/// instructions like this:
1735/// * %F = firstOpcode SrcTy %x to MidTy
1736/// * %S = secondOpcode MidTy %F to DstTy
1737/// The function returns a resultOpcode so these two casts can be replaced with:
1738/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1739/// If no such cast is permited, the function returns 0.
1740unsigned CastInst::isEliminableCastPair(
1741 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1742 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1743{
1744 // Define the 144 possibilities for these two cast instructions. The values
1745 // in this matrix determine what to do in a given situation and select the
1746 // case in the switch below. The rows correspond to firstOp, the columns
1747 // correspond to secondOp. In looking at the table below, keep in mind
1748 // the following cast properties:
1749 //
1750 // Size Compare Source Destination
1751 // Operator Src ? Size Type Sign Type Sign
1752 // -------- ------------ ------------------- ---------------------
1753 // TRUNC > Integer Any Integral Any
1754 // ZEXT < Integral Unsigned Integer Any
1755 // SEXT < Integral Signed Integer Any
1756 // FPTOUI n/a FloatPt n/a Integral Unsigned
1757 // FPTOSI n/a FloatPt n/a Integral Signed
1758 // UITOFP n/a Integral Unsigned FloatPt n/a
1759 // SITOFP n/a Integral Signed FloatPt n/a
1760 // FPTRUNC > FloatPt n/a FloatPt n/a
1761 // FPEXT < FloatPt n/a FloatPt n/a
1762 // PTRTOINT n/a Pointer n/a Integral Unsigned
1763 // INTTOPTR n/a Integral Unsigned Pointer n/a
1764 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001765 //
1766 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1767 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1768 // into "fptoui double to ulong", but this loses information about the range
1769 // of the produced value (we no longer know the top-part is all zeros).
1770 // Further this conversion is often much more expensive for typical hardware,
1771 // and causes issues when building libgcc. We disallow fptosi+sext for the
1772 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001773 const unsigned numCastOps =
1774 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1775 static const uint8_t CastResults[numCastOps][numCastOps] = {
1776 // T F F U S F F P I B -+
1777 // R Z S P P I I T P 2 N T |
1778 // U E E 2 2 2 2 R E I T C +- secondOp
1779 // N X X U S F F N X N 2 V |
1780 // C T T I I P P C T T P T -+
1781 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1782 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1783 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001784 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1785 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001786 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1787 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1788 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1789 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1790 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1791 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1792 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1793 };
1794
1795 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1796 [secondOp-Instruction::CastOpsBegin];
1797 switch (ElimCase) {
1798 case 0:
1799 // categorically disallowed
1800 return 0;
1801 case 1:
1802 // allowed, use first cast's opcode
1803 return firstOp;
1804 case 2:
1805 // allowed, use second cast's opcode
1806 return secondOp;
1807 case 3:
1808 // no-op cast in second op implies firstOp as long as the DestTy
1809 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001810 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001811 return firstOp;
1812 return 0;
1813 case 4:
1814 // no-op cast in second op implies firstOp as long as the DestTy
1815 // is floating point
1816 if (DstTy->isFloatingPoint())
1817 return firstOp;
1818 return 0;
1819 case 5:
1820 // no-op cast in first op implies secondOp as long as the SrcTy
1821 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001822 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001823 return secondOp;
1824 return 0;
1825 case 6:
1826 // no-op cast in first op implies secondOp as long as the SrcTy
1827 // is a floating point
1828 if (SrcTy->isFloatingPoint())
1829 return secondOp;
1830 return 0;
1831 case 7: {
1832 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1833 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1834 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1835 if (MidSize >= PtrSize)
1836 return Instruction::BitCast;
1837 return 0;
1838 }
1839 case 8: {
1840 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1841 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1842 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1843 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1844 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1845 if (SrcSize == DstSize)
1846 return Instruction::BitCast;
1847 else if (SrcSize < DstSize)
1848 return firstOp;
1849 return secondOp;
1850 }
1851 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1852 return Instruction::ZExt;
1853 case 10:
1854 // fpext followed by ftrunc is allowed if the bit size returned to is
1855 // the same as the original, in which case its just a bitcast
1856 if (SrcTy == DstTy)
1857 return Instruction::BitCast;
1858 return 0; // If the types are not the same we can't eliminate it.
1859 case 11:
1860 // bitcast followed by ptrtoint is allowed as long as the bitcast
1861 // is a pointer to pointer cast.
1862 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1863 return secondOp;
1864 return 0;
1865 case 12:
1866 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1867 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1868 return firstOp;
1869 return 0;
1870 case 13: {
1871 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1872 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1873 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1874 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1875 if (SrcSize <= PtrSize && SrcSize == DstSize)
1876 return Instruction::BitCast;
1877 return 0;
1878 }
1879 case 99:
1880 // cast combination can't happen (error in input). This is for all cases
1881 // where the MidTy is not the same for the two cast instructions.
1882 assert(!"Invalid Cast Combination");
1883 return 0;
1884 default:
1885 assert(!"Error in CastResults table!!!");
1886 return 0;
1887 }
1888 return 0;
1889}
1890
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001891CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001892 const std::string &Name, Instruction *InsertBefore) {
1893 // Construct and return the appropriate CastInst subclass
1894 switch (op) {
1895 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1896 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1897 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1898 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1899 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1900 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1901 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1902 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1903 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1904 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1905 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1906 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1907 default:
1908 assert(!"Invalid opcode provided");
1909 }
1910 return 0;
1911}
1912
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001913CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001914 const std::string &Name, BasicBlock *InsertAtEnd) {
1915 // Construct and return the appropriate CastInst subclass
1916 switch (op) {
1917 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1918 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1919 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1920 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1921 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1922 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1923 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1924 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1925 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1926 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1927 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1928 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1929 default:
1930 assert(!"Invalid opcode provided");
1931 }
1932 return 0;
1933}
1934
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001935CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001936 const std::string &Name,
1937 Instruction *InsertBefore) {
1938 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001939 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1940 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00001941}
1942
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001943CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001944 const std::string &Name,
1945 BasicBlock *InsertAtEnd) {
1946 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001947 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1948 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00001949}
1950
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001951CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001952 const std::string &Name,
1953 Instruction *InsertBefore) {
1954 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001955 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1956 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00001957}
1958
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001959CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001960 const std::string &Name,
1961 BasicBlock *InsertAtEnd) {
1962 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001963 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1964 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00001965}
1966
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001967CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001968 const std::string &Name,
1969 Instruction *InsertBefore) {
1970 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001971 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1972 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00001973}
1974
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001975CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001976 const std::string &Name,
1977 BasicBlock *InsertAtEnd) {
1978 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001979 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1980 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00001981}
1982
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001983CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001984 const std::string &Name,
1985 BasicBlock *InsertAtEnd) {
1986 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001987 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001988 "Invalid cast");
1989
Chris Lattner03c49532007-01-15 02:27:26 +00001990 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001991 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1992 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001993}
1994
1995/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001996CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001997 const std::string &Name,
1998 Instruction *InsertBefore) {
1999 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002000 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002001 "Invalid cast");
2002
Chris Lattner03c49532007-01-15 02:27:26 +00002003 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002004 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2005 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002006}
2007
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002008CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002009 bool isSigned, const std::string &Name,
2010 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00002011 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00002012 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2013 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2014 Instruction::CastOps opcode =
2015 (SrcBits == DstBits ? Instruction::BitCast :
2016 (SrcBits > DstBits ? Instruction::Trunc :
2017 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002018 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002019}
2020
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002021CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002022 bool isSigned, const std::string &Name,
2023 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00002024 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00002025 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2026 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2027 Instruction::CastOps opcode =
2028 (SrcBits == DstBits ? Instruction::BitCast :
2029 (SrcBits > DstBits ? Instruction::Trunc :
2030 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002031 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002032}
2033
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002034CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002035 const std::string &Name,
2036 Instruction *InsertBefore) {
2037 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
2038 "Invalid cast");
2039 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2040 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2041 Instruction::CastOps opcode =
2042 (SrcBits == DstBits ? Instruction::BitCast :
2043 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002044 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002045}
2046
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002047CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002048 const std::string &Name,
2049 BasicBlock *InsertAtEnd) {
2050 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
2051 "Invalid cast");
2052 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2053 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2054 Instruction::CastOps opcode =
2055 (SrcBits == DstBits ? Instruction::BitCast :
2056 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002057 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002058}
2059
Duncan Sands55e50902008-01-06 10:12:28 +00002060// Check whether it is valid to call getCastOpcode for these types.
2061// This routine must be kept in sync with getCastOpcode.
2062bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2063 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2064 return false;
2065
2066 if (SrcTy == DestTy)
2067 return true;
2068
2069 // Get the bit sizes, we'll need these
2070 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2071 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2072
2073 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00002074 if (DestTy->isInteger()) { // Casting to integral
2075 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002076 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002077 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002078 return true;
2079 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002080 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002081 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002082 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002083 return isa<PointerType>(SrcTy);
2084 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002085 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2086 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002087 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002088 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002089 return true;
2090 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002091 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002092 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002093 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002094 return false;
2095 }
2096 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002097 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002098 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002099 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002100 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002101 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002102 return DestPTy->getBitWidth() == SrcBits;
2103 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002104 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2105 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002106 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002107 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002108 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002109 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002110 return false;
2111 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002112 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002113 return false;
2114 }
2115}
2116
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002117// Provide a way to get a "cast" where the cast opcode is inferred from the
2118// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002119// logic in the castIsValid function below. This axiom should hold:
2120// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2121// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002122// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002123// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002124Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002125CastInst::getCastOpcode(
2126 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002127 // Get the bit sizes, we'll need these
2128 const Type *SrcTy = Src->getType();
Dan Gohmanfead7972007-05-11 21:43:24 +00002129 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2130 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002131
Duncan Sands55e50902008-01-06 10:12:28 +00002132 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2133 "Only first class types are castable!");
2134
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002135 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002136 if (DestTy->isInteger()) { // Casting to integral
2137 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002138 if (DestBits < SrcBits)
2139 return Trunc; // int -> smaller int
2140 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002141 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002142 return SExt; // signed -> SEXT
2143 else
2144 return ZExt; // unsigned -> ZEXT
2145 } else {
2146 return BitCast; // Same size, No-op cast
2147 }
2148 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002149 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002150 return FPToSI; // FP -> sint
2151 else
2152 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002153 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002154 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002155 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002156 return BitCast; // Same size, no-op cast
2157 } else {
2158 assert(isa<PointerType>(SrcTy) &&
2159 "Casting from a value that is not first-class type");
2160 return PtrToInt; // ptr -> int
2161 }
2162 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002163 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002164 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002165 return SIToFP; // sint -> FP
2166 else
2167 return UIToFP; // uint -> FP
2168 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2169 if (DestBits < SrcBits) {
2170 return FPTrunc; // FP -> smaller FP
2171 } else if (DestBits > SrcBits) {
2172 return FPExt; // FP -> larger FP
2173 } else {
2174 return BitCast; // same size, no-op cast
2175 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002176 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002177 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002178 "Casting vector to floating point of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002179 return BitCast; // same size, no-op cast
2180 } else {
2181 assert(0 && "Casting pointer or non-first class to float");
2182 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002183 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2184 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002185 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002186 "Casting vector to vector of different widths");
2187 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002188 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002189 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002190 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002191 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002192 }
2193 } else if (isa<PointerType>(DestTy)) {
2194 if (isa<PointerType>(SrcTy)) {
2195 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002196 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002197 return IntToPtr; // int -> ptr
2198 } else {
2199 assert(!"Casting pointer to other than pointer or int");
2200 }
2201 } else {
2202 assert(!"Casting to type that is not first-class");
2203 }
2204
2205 // If we fall through to here we probably hit an assertion cast above
2206 // and assertions are not turned on. Anything we return is an error, so
2207 // BitCast is as good a choice as any.
2208 return BitCast;
2209}
2210
2211//===----------------------------------------------------------------------===//
2212// CastInst SubClass Constructors
2213//===----------------------------------------------------------------------===//
2214
2215/// Check that the construction parameters for a CastInst are correct. This
2216/// could be broken out into the separate constructors but it is useful to have
2217/// it in one place and to eliminate the redundant code for getting the sizes
2218/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002219bool
2220CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002221
2222 // Check for type sanity on the arguments
2223 const Type *SrcTy = S->getType();
2224 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2225 return false;
2226
2227 // Get the size of the types in bits, we'll need this later
2228 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2229 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
2230
2231 // Switch on the opcode provided
2232 switch (op) {
2233 default: return false; // This is an input error
2234 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00002235 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002236 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002237 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002238 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002239 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002240 case Instruction::FPTrunc:
2241 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2242 SrcBitSize > DstBitSize;
2243 case Instruction::FPExt:
2244 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2245 SrcBitSize < DstBitSize;
2246 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002247 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002248 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2249 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2250 return SVTy->getElementType()->isInteger() &&
2251 DVTy->getElementType()->isFloatingPoint() &&
2252 SVTy->getNumElements() == DVTy->getNumElements();
2253 }
2254 }
Chris Lattner03c49532007-01-15 02:27:26 +00002255 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002256 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002257 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002258 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2259 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2260 return SVTy->getElementType()->isFloatingPoint() &&
2261 DVTy->getElementType()->isInteger() &&
2262 SVTy->getNumElements() == DVTy->getNumElements();
2263 }
2264 }
Chris Lattner03c49532007-01-15 02:27:26 +00002265 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002266 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002267 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002268 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002269 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002270 case Instruction::BitCast:
2271 // BitCast implies a no-op cast of type only. No bits change.
2272 // However, you can't cast pointers to anything but pointers.
2273 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2274 return false;
2275
Duncan Sands55e50902008-01-06 10:12:28 +00002276 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002277 // these cases, the cast is okay if the source and destination bit widths
2278 // are identical.
2279 return SrcBitSize == DstBitSize;
2280 }
2281}
2282
2283TruncInst::TruncInst(
2284 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2285) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002286 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002287}
2288
2289TruncInst::TruncInst(
2290 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2291) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002292 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002293}
2294
2295ZExtInst::ZExtInst(
2296 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2297) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002298 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002299}
2300
2301ZExtInst::ZExtInst(
2302 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2303) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002304 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002305}
2306SExtInst::SExtInst(
2307 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2308) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002309 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002310}
2311
Jeff Cohencc08c832006-12-02 02:22:01 +00002312SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002313 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2314) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002315 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002316}
2317
2318FPTruncInst::FPTruncInst(
2319 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2320) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002321 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002322}
2323
2324FPTruncInst::FPTruncInst(
2325 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2326) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002327 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002328}
2329
2330FPExtInst::FPExtInst(
2331 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2332) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002333 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002334}
2335
2336FPExtInst::FPExtInst(
2337 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2338) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002339 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002340}
2341
2342UIToFPInst::UIToFPInst(
2343 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2344) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002345 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002346}
2347
2348UIToFPInst::UIToFPInst(
2349 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2350) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002351 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002352}
2353
2354SIToFPInst::SIToFPInst(
2355 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2356) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002357 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002358}
2359
2360SIToFPInst::SIToFPInst(
2361 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2362) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002363 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002364}
2365
2366FPToUIInst::FPToUIInst(
2367 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2368) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002369 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002370}
2371
2372FPToUIInst::FPToUIInst(
2373 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2374) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002375 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002376}
2377
2378FPToSIInst::FPToSIInst(
2379 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2380) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002381 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002382}
2383
2384FPToSIInst::FPToSIInst(
2385 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2386) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002387 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002388}
2389
2390PtrToIntInst::PtrToIntInst(
2391 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2392) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002393 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002394}
2395
2396PtrToIntInst::PtrToIntInst(
2397 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2398) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002399 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002400}
2401
2402IntToPtrInst::IntToPtrInst(
2403 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2404) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002405 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002406}
2407
2408IntToPtrInst::IntToPtrInst(
2409 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2410) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002411 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002412}
2413
2414BitCastInst::BitCastInst(
2415 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2416) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002417 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002418}
2419
2420BitCastInst::BitCastInst(
2421 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2422) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002423 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002424}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002425
2426//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002427// CmpInst Classes
2428//===----------------------------------------------------------------------===//
2429
Nate Begemand2195702008-05-12 19:01:56 +00002430CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2431 Value *LHS, Value *RHS, const std::string &Name,
2432 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002433 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002434 OperandTraits<CmpInst>::op_begin(this),
2435 OperandTraits<CmpInst>::operands(this),
2436 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002437 Op<0>() = LHS;
2438 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002439 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002440 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002441}
Gabor Greiff6caff662008-05-10 08:32:32 +00002442
Nate Begemand2195702008-05-12 19:01:56 +00002443CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2444 Value *LHS, Value *RHS, const std::string &Name,
2445 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002446 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002447 OperandTraits<CmpInst>::op_begin(this),
2448 OperandTraits<CmpInst>::operands(this),
2449 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002450 Op<0>() = LHS;
2451 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002452 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002453 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002454}
2455
2456CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002457CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002458 const std::string &Name, Instruction *InsertBefore) {
2459 if (Op == Instruction::ICmp) {
Nate Begemand2195702008-05-12 19:01:56 +00002460 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
Reid Spencerd9436b62006-11-20 01:22:35 +00002461 InsertBefore);
2462 }
Nate Begemand2195702008-05-12 19:01:56 +00002463 if (Op == Instruction::FCmp) {
2464 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2465 InsertBefore);
2466 }
2467 if (Op == Instruction::VICmp) {
2468 return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2469 InsertBefore);
2470 }
2471 return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2472 InsertBefore);
Reid Spencerd9436b62006-11-20 01:22:35 +00002473}
2474
2475CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002476CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002477 const std::string &Name, BasicBlock *InsertAtEnd) {
2478 if (Op == Instruction::ICmp) {
Nate Begemand2195702008-05-12 19:01:56 +00002479 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
Reid Spencerd9436b62006-11-20 01:22:35 +00002480 InsertAtEnd);
2481 }
Nate Begemand2195702008-05-12 19:01:56 +00002482 if (Op == Instruction::FCmp) {
2483 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2484 InsertAtEnd);
2485 }
2486 if (Op == Instruction::VICmp) {
2487 return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2488 InsertAtEnd);
2489 }
2490 return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2491 InsertAtEnd);
Reid Spencerd9436b62006-11-20 01:22:35 +00002492}
2493
2494void CmpInst::swapOperands() {
2495 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2496 IC->swapOperands();
2497 else
2498 cast<FCmpInst>(this)->swapOperands();
2499}
2500
2501bool CmpInst::isCommutative() {
2502 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2503 return IC->isCommutative();
2504 return cast<FCmpInst>(this)->isCommutative();
2505}
2506
2507bool CmpInst::isEquality() {
2508 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2509 return IC->isEquality();
2510 return cast<FCmpInst>(this)->isEquality();
2511}
2512
2513
Dan Gohman4e724382008-05-31 02:47:54 +00002514CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002515 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002516 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002517 case ICMP_EQ: return ICMP_NE;
2518 case ICMP_NE: return ICMP_EQ;
2519 case ICMP_UGT: return ICMP_ULE;
2520 case ICMP_ULT: return ICMP_UGE;
2521 case ICMP_UGE: return ICMP_ULT;
2522 case ICMP_ULE: return ICMP_UGT;
2523 case ICMP_SGT: return ICMP_SLE;
2524 case ICMP_SLT: return ICMP_SGE;
2525 case ICMP_SGE: return ICMP_SLT;
2526 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002527
Dan Gohman4e724382008-05-31 02:47:54 +00002528 case FCMP_OEQ: return FCMP_UNE;
2529 case FCMP_ONE: return FCMP_UEQ;
2530 case FCMP_OGT: return FCMP_ULE;
2531 case FCMP_OLT: return FCMP_UGE;
2532 case FCMP_OGE: return FCMP_ULT;
2533 case FCMP_OLE: return FCMP_UGT;
2534 case FCMP_UEQ: return FCMP_ONE;
2535 case FCMP_UNE: return FCMP_OEQ;
2536 case FCMP_UGT: return FCMP_OLE;
2537 case FCMP_ULT: return FCMP_OGE;
2538 case FCMP_UGE: return FCMP_OLT;
2539 case FCMP_ULE: return FCMP_OGT;
2540 case FCMP_ORD: return FCMP_UNO;
2541 case FCMP_UNO: return FCMP_ORD;
2542 case FCMP_TRUE: return FCMP_FALSE;
2543 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002544 }
2545}
2546
Reid Spencer266e42b2006-12-23 06:05:41 +00002547ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2548 switch (pred) {
2549 default: assert(! "Unknown icmp predicate!");
2550 case ICMP_EQ: case ICMP_NE:
2551 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2552 return pred;
2553 case ICMP_UGT: return ICMP_SGT;
2554 case ICMP_ULT: return ICMP_SLT;
2555 case ICMP_UGE: return ICMP_SGE;
2556 case ICMP_ULE: return ICMP_SLE;
2557 }
2558}
2559
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002560ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2561 switch (pred) {
2562 default: assert(! "Unknown icmp predicate!");
2563 case ICMP_EQ: case ICMP_NE:
2564 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2565 return pred;
2566 case ICMP_SGT: return ICMP_UGT;
2567 case ICMP_SLT: return ICMP_ULT;
2568 case ICMP_SGE: return ICMP_UGE;
2569 case ICMP_SLE: return ICMP_ULE;
2570 }
2571}
2572
Reid Spencer266e42b2006-12-23 06:05:41 +00002573bool ICmpInst::isSignedPredicate(Predicate pred) {
2574 switch (pred) {
2575 default: assert(! "Unknown icmp predicate!");
2576 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2577 return true;
2578 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2579 case ICMP_UGE: case ICMP_ULE:
2580 return false;
2581 }
2582}
2583
Reid Spencer0286bc12007-02-28 22:00:54 +00002584/// Initialize a set of values that all satisfy the condition with C.
2585///
2586ConstantRange
2587ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2588 APInt Lower(C);
2589 APInt Upper(C);
2590 uint32_t BitWidth = C.getBitWidth();
2591 switch (pred) {
2592 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2593 case ICmpInst::ICMP_EQ: Upper++; break;
2594 case ICmpInst::ICMP_NE: Lower++; break;
2595 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2596 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2597 case ICmpInst::ICMP_UGT:
2598 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2599 break;
2600 case ICmpInst::ICMP_SGT:
2601 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2602 break;
2603 case ICmpInst::ICMP_ULE:
2604 Lower = APInt::getMinValue(BitWidth); Upper++;
2605 break;
2606 case ICmpInst::ICMP_SLE:
2607 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2608 break;
2609 case ICmpInst::ICMP_UGE:
2610 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2611 break;
2612 case ICmpInst::ICMP_SGE:
2613 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2614 break;
2615 }
2616 return ConstantRange(Lower, Upper);
2617}
2618
Dan Gohman4e724382008-05-31 02:47:54 +00002619CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002620 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002621 default: assert(!"Unknown cmp predicate!");
2622 case ICMP_EQ: case ICMP_NE:
2623 return pred;
2624 case ICMP_SGT: return ICMP_SLT;
2625 case ICMP_SLT: return ICMP_SGT;
2626 case ICMP_SGE: return ICMP_SLE;
2627 case ICMP_SLE: return ICMP_SGE;
2628 case ICMP_UGT: return ICMP_ULT;
2629 case ICMP_ULT: return ICMP_UGT;
2630 case ICMP_UGE: return ICMP_ULE;
2631 case ICMP_ULE: return ICMP_UGE;
2632
Reid Spencerd9436b62006-11-20 01:22:35 +00002633 case FCMP_FALSE: case FCMP_TRUE:
2634 case FCMP_OEQ: case FCMP_ONE:
2635 case FCMP_UEQ: case FCMP_UNE:
2636 case FCMP_ORD: case FCMP_UNO:
2637 return pred;
2638 case FCMP_OGT: return FCMP_OLT;
2639 case FCMP_OLT: return FCMP_OGT;
2640 case FCMP_OGE: return FCMP_OLE;
2641 case FCMP_OLE: return FCMP_OGE;
2642 case FCMP_UGT: return FCMP_ULT;
2643 case FCMP_ULT: return FCMP_UGT;
2644 case FCMP_UGE: return FCMP_ULE;
2645 case FCMP_ULE: return FCMP_UGE;
2646 }
2647}
2648
Reid Spencer266e42b2006-12-23 06:05:41 +00002649bool CmpInst::isUnsigned(unsigned short predicate) {
2650 switch (predicate) {
2651 default: return false;
2652 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2653 case ICmpInst::ICMP_UGE: return true;
2654 }
2655}
2656
2657bool CmpInst::isSigned(unsigned short predicate){
2658 switch (predicate) {
2659 default: return false;
2660 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2661 case ICmpInst::ICMP_SGE: return true;
2662 }
2663}
2664
2665bool CmpInst::isOrdered(unsigned short predicate) {
2666 switch (predicate) {
2667 default: return false;
2668 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2669 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2670 case FCmpInst::FCMP_ORD: return true;
2671 }
2672}
2673
2674bool CmpInst::isUnordered(unsigned short predicate) {
2675 switch (predicate) {
2676 default: return false;
2677 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2678 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2679 case FCmpInst::FCMP_UNO: return true;
2680 }
2681}
2682
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002683//===----------------------------------------------------------------------===//
2684// SwitchInst Implementation
2685//===----------------------------------------------------------------------===//
2686
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002687void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002688 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002689 ReservedSpace = 2+NumCases*2;
2690 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002691 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002692
Gabor Greif2d3024d2008-05-26 21:33:52 +00002693 OperandList[0] = Value;
2694 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002695}
2696
Chris Lattner2195fc42007-02-24 00:55:48 +00002697/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2698/// switch on and a default destination. The number of additional cases can
2699/// be specified here to make memory allocation more efficient. This
2700/// constructor can also autoinsert before another instruction.
2701SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2702 Instruction *InsertBefore)
2703 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2704 init(Value, Default, NumCases);
2705}
2706
2707/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2708/// switch on and a default destination. The number of additional cases can
2709/// be specified here to make memory allocation more efficient. This
2710/// constructor also autoinserts at the end of the specified BasicBlock.
2711SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2712 BasicBlock *InsertAtEnd)
2713 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2714 init(Value, Default, NumCases);
2715}
2716
Misha Brukmanb1c93172005-04-21 23:48:37 +00002717SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002718 : TerminatorInst(Type::VoidTy, Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002719 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002720 Use *OL = OperandList, *InOL = SI.OperandList;
2721 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002722 OL[i] = InOL[i];
2723 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002724 }
2725}
2726
Gordon Henriksen14a55692007-12-10 02:14:30 +00002727SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002728 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002729}
2730
2731
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002732/// addCase - Add an entry to the switch instruction...
2733///
Chris Lattner47ac1872005-02-24 05:32:09 +00002734void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002735 unsigned OpNo = NumOperands;
2736 if (OpNo+2 > ReservedSpace)
2737 resizeOperands(0); // Get more space!
2738 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002739 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002740 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002741 OperandList[OpNo] = OnVal;
2742 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002743}
2744
2745/// removeCase - This method removes the specified successor from the switch
2746/// instruction. Note that this cannot be used to remove the default
2747/// destination (successor #0).
2748///
2749void SwitchInst::removeCase(unsigned idx) {
2750 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002751 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2752
2753 unsigned NumOps = getNumOperands();
2754 Use *OL = OperandList;
2755
2756 // Move everything after this operand down.
2757 //
2758 // FIXME: we could just swap with the end of the list, then erase. However,
2759 // client might not expect this to happen. The code as it is thrashes the
2760 // use/def lists, which is kinda lame.
2761 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2762 OL[i-2] = OL[i];
2763 OL[i-2+1] = OL[i+1];
2764 }
2765
2766 // Nuke the last value.
2767 OL[NumOps-2].set(0);
2768 OL[NumOps-2+1].set(0);
2769 NumOperands = NumOps-2;
2770}
2771
2772/// resizeOperands - resize operands - This adjusts the length of the operands
2773/// list according to the following behavior:
2774/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00002775/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002776/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2777/// 3. If NumOps == NumOperands, trim the reserved space.
2778///
2779void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002780 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002781 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002782 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002783 } else if (NumOps*2 > NumOperands) {
2784 // No resize needed.
2785 if (ReservedSpace >= NumOps) return;
2786 } else if (NumOps == NumOperands) {
2787 if (ReservedSpace == NumOps) return;
2788 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002789 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002790 }
2791
2792 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002793 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002794 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00002795 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002796 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002797 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002798 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002799 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002800}
2801
2802
2803BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2804 return getSuccessor(idx);
2805}
2806unsigned SwitchInst::getNumSuccessorsV() const {
2807 return getNumSuccessors();
2808}
2809void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2810 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002811}
Chris Lattnerf22be932004-10-15 23:52:53 +00002812
Chris Lattnerf22be932004-10-15 23:52:53 +00002813// Define these methods here so vtables don't get emitted into every translation
2814// unit that uses these classes.
2815
2816GetElementPtrInst *GetElementPtrInst::clone() const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002817 return new(getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00002818}
2819
2820BinaryOperator *BinaryOperator::clone() const {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002821 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00002822}
2823
Chris Lattner0b490b02007-08-24 20:48:18 +00002824FCmpInst* FCmpInst::clone() const {
Gabor Greiff6caff662008-05-10 08:32:32 +00002825 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Chris Lattner0b490b02007-08-24 20:48:18 +00002826}
2827ICmpInst* ICmpInst::clone() const {
Gabor Greiff6caff662008-05-10 08:32:32 +00002828 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00002829}
2830
Nate Begemand2195702008-05-12 19:01:56 +00002831VFCmpInst* VFCmpInst::clone() const {
2832 return new VFCmpInst(getPredicate(), Op<0>(), Op<1>());
2833}
2834VICmpInst* VICmpInst::clone() const {
2835 return new VICmpInst(getPredicate(), Op<0>(), Op<1>());
2836}
2837
Dan Gohman0752bff2008-05-23 00:36:11 +00002838ExtractValueInst *ExtractValueInst::clone() const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002839 return new ExtractValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002840}
2841InsertValueInst *InsertValueInst::clone() const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002842 return new InsertValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002843}
2844
2845
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002846MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2847AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2848FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2849LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2850StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2851CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2852CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2853CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2854CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2855CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2856CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2857CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2858CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2859CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2860CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2861CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2862CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002863CallInst *CallInst::clone() const {
2864 return new(getNumOperands()) CallInst(*this);
2865}
2866SelectInst *SelectInst::clone() const {
2867 return new(getNumOperands()) SelectInst(*this);
2868}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002869VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2870
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002871ExtractElementInst *ExtractElementInst::clone() const {
2872 return new ExtractElementInst(*this);
2873}
2874InsertElementInst *InsertElementInst::clone() const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002875 return InsertElementInst::Create(*this);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002876}
2877ShuffleVectorInst *ShuffleVectorInst::clone() const {
2878 return new ShuffleVectorInst(*this);
2879}
Chris Lattnerf22be932004-10-15 23:52:53 +00002880PHINode *PHINode::clone() const { return new PHINode(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002881ReturnInst *ReturnInst::clone() const {
2882 return new(getNumOperands()) ReturnInst(*this);
2883}
2884BranchInst *BranchInst::clone() const {
2885 return new(getNumOperands()) BranchInst(*this);
2886}
Gabor Greiff6caff662008-05-10 08:32:32 +00002887SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002888InvokeInst *InvokeInst::clone() const {
2889 return new(getNumOperands()) InvokeInst(*this);
2890}
Chris Lattnerf22be932004-10-15 23:52:53 +00002891UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002892UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}