1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
|
//===- ReplacePtrsWithInts.cpp - Convert pointer values to integer values--===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This pass strips out aggregate pointer types and replaces them with
// the integer type iPTR, which is i32 for PNaCl (though this pass
// will allow iPTR to be i64 if the DataLayout specifies 64-bit
// pointers).
//
// The pass converts IR to the following normal form:
//
// All inttoptr and ptrtoint instructions use the same integer size
// (iPTR), so they do not implicitly truncate or zero-extend.
//
// alloca always has the result type i8*.
//
// Pointer types only appear in the following instructions:
// * loads and stores: the pointer operand is a NormalizedPtr.
// * function calls: the function operand is a NormalizedPtr.
// * intrinsic calls: any pointer arguments are NormalizedPtrs.
// * alloca
// * bitcast and inttoptr: only used as part of a NormalizedPtr.
// * ptrtoint: the operand is an InherentPtr.
//
// Where an InherentPtr is defined as a pointer value that is:
// * an alloca;
// * a GlobalValue (a function or global variable); or
// * an intrinsic call.
//
// And a NormalizedPtr is defined as a pointer value that is:
// * an inttoptr instruction;
// * an InherentPtr; or
// * a bitcast of an InherentPtr.
//
// This pass currently strips out lifetime markers (that is, calls to
// the llvm.lifetime.start/end intrinsics) and invariant markers
// (calls to llvm.invariant.start/end).
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/Pass.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/NaCl.h"
using namespace llvm;
namespace {
// This is a ModulePass because the pass must recreate functions in
// order to change their argument and return types.
struct ReplacePtrsWithInts : public ModulePass {
static char ID; // Pass identification, replacement for typeid
ReplacePtrsWithInts() : ModulePass(ID) {
initializeReplacePtrsWithIntsPass(*PassRegistry::getPassRegistry());
}
virtual bool runOnModule(Module &M);
};
// FunctionConverter stores the state for mapping old instructions
// (of pointer type) to converted instructions (of integer type)
// within a function, and provides methods for doing the conversion.
class FunctionConverter {
// Int type that pointer types are to be replaced with, typically i32.
Type *IntPtrType;
struct RewrittenVal {
RewrittenVal(): Placeholder(NULL), NewIntVal(NULL) {}
Value *Placeholder;
Value *NewIntVal;
};
// Maps from old values (of pointer type) to converted values (of
// IntPtrType type).
DenseMap<Value *, RewrittenVal> RewriteMap;
public:
FunctionConverter(Type *IntPtrType) : IntPtrType(IntPtrType) {}
// Returns the normalized version of the given type, converting
// pointer types to IntPtrType.
Type *convertType(Type *Ty);
// Returns the normalized version of the given function type by
// normalizing the function's argument types.
FunctionType *convertFuncType(FunctionType *FTy);
// Records that 'To' is the normalized version of 'From'. If 'To'
// is not of pointer type, no type conversion is required, so this
// can take the short cut of replacing 'To' with 'From'.
void recordConverted(Value *From, Value *To);
void recordConvertedAndErase(Instruction *From, Value *To);
// Returns Val with no-op casts (those that convert between
// IntPtrType and pointer types) stripped off.
Value *stripNoopCasts(Value *Val);
// Returns the normalized version of the given value.
//
// If the conversion of Val has been deferred, this returns a
// placeholder object, which will later be replaceAllUsesWith'd to
// the final value. Since replaceAllUsesWith does not work on
// references by metadata nodes, this can be bypassed using
// BypassPlaceholder to get the real converted value, assuming it
// is available.
Value *convert(Value *Val, bool BypassPlaceholder = false);
// Returns the NormalizedPtr form of the given pointer value.
// Inserts conversion instructions at InsertPt.
Value *convertBackToPtr(Value *Val, Instruction *InsertPt);
// Returns the NormalizedPtr form of the given function pointer.
// Inserts conversion instructions at InsertPt.
Value *convertFunctionPtr(Value *Callee, Instruction *InsertPt);
// Converts an instruction without recreating it, by wrapping its
// operands and result.
void convertInPlace(Instruction *Inst);
void eraseReplacedInstructions();
// List of instructions whose deletion has been deferred.
SmallVector<Instruction *, 20> ToErase;
};
}
Type *FunctionConverter::convertType(Type *Ty) {
if (Ty->isPointerTy())
return IntPtrType;
return Ty;
}
FunctionType *FunctionConverter::convertFuncType(FunctionType *FTy) {
SmallVector<Type *, 8> ArgTypes;
for (FunctionType::param_iterator ArgTy = FTy->param_begin(),
E = FTy->param_end(); ArgTy != E; ++ArgTy) {
ArgTypes.push_back(convertType(*ArgTy));
}
return FunctionType::get(convertType(FTy->getReturnType()), ArgTypes,
FTy->isVarArg());
}
void FunctionConverter::recordConverted(Value *From, Value *To) {
if (!From->getType()->isPointerTy()) {
From->replaceAllUsesWith(To);
return;
}
RewrittenVal *RV = &RewriteMap[From];
assert(!RV->NewIntVal);
RV->NewIntVal = To;
}
void FunctionConverter::recordConvertedAndErase(Instruction *From, Value *To) {
recordConverted(From, To);
// There may still be references to this value, so defer deleting it.
ToErase.push_back(From);
}
Value *FunctionConverter::stripNoopCasts(Value *Val) {
SmallPtrSet<Value *, 4> Visited;
for (;;) {
if (!Visited.insert(Val)) {
// It is possible to get a circular reference in unreachable
// basic blocks. Handle this case for completeness.
return UndefValue::get(Val->getType());
}
if (CastInst *Cast = dyn_cast<CastInst>(Val)) {
Value *Src = Cast->getOperand(0);
if ((isa<BitCastInst>(Cast) && Cast->getType()->isPointerTy()) ||
(isa<PtrToIntInst>(Cast) && Cast->getType() == IntPtrType) ||
(isa<IntToPtrInst>(Cast) && Src->getType() == IntPtrType)) {
Val = Src;
continue;
}
}
return Val;
}
}
Value *FunctionConverter::convert(Value *Val, bool BypassPlaceholder) {
Val = stripNoopCasts(Val);
if (!Val->getType()->isPointerTy())
return Val;
if (Constant *C = dyn_cast<Constant>(Val))
return ConstantExpr::getPtrToInt(C, IntPtrType);
RewrittenVal *RV = &RewriteMap[Val];
if (BypassPlaceholder) {
assert(RV->NewIntVal);
return RV->NewIntVal;
}
if (!RV->Placeholder)
RV->Placeholder = new Argument(convertType(Val->getType()));
return RV->Placeholder;
}
Value *FunctionConverter::convertBackToPtr(Value *Val, Instruction *InsertPt) {
Type *NewTy =
convertType(Val->getType()->getPointerElementType())->getPointerTo();
return new IntToPtrInst(convert(Val), NewTy, "", InsertPt);
}
Value *FunctionConverter::convertFunctionPtr(Value *Callee,
Instruction *InsertPt) {
FunctionType *FuncType = cast<FunctionType>(
Callee->getType()->getPointerElementType());
return new IntToPtrInst(convert(Callee),
convertFuncType(FuncType)->getPointerTo(),
"", InsertPt);
}
static bool ShouldLeaveAlone(Value *V) {
if (Function *F = dyn_cast<Function>(V))
return F->isIntrinsic();
if (isa<InlineAsm>(
|