1 //===- CheriSetBounds.h - Functions to log information on CSetBounds ------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This family of functions perform various local transformations to the
11 // program.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_TRANSFORMS_UTILS_CHERISETBOUNDS_H
16 #define LLVM_TRANSFORMS_UTILS_CHERISETBOUNDS_H
17 
18 #include "llvm/IR/Instructions.h"
19 #include "llvm/Support/CheriSetBounds.h"
20 #include "llvm/Transforms/Utils.h"
21 
22 namespace llvm {
23 
24 class Value;
25 class Instruction;
26 
27 namespace cheri {
28 
29 // This probably exists somewhere else
inferConstantValue(Value * V)30 inline Optional<uint64_t> inferConstantValue(Value *V) {
31   Optional<uint64_t> Result;
32   if (auto CI = dyn_cast_or_null<ConstantInt>(V)) {
33     Result = CI->getSExtValue();
34   } else {
35     // TODO: use KnownBits to infer something?
36   }
37   return Result;
38 }
39 
40 inline void addSetBoundsStats(Align KnownAlignment, Value *Length,
41                               StringRef Pass, SetBoundsPointerSource Kind,
42                               const Twine &Details, std::string SourceLoc,
43                               Optional<uint64_t> SizeMultipleOf = None) {
44   CSetBoundsStats->add(KnownAlignment, inferConstantValue(Length), Pass, Kind,
45                        Details, std::move(SourceLoc), SizeMultipleOf);
46 }
47 
inferPointerSource(const Value * V)48 inline SetBoundsPointerSource inferPointerSource(const Value *V) {
49   // look through casts and find out if they are global/alloca/unknown
50   return SetBoundsPointerSource::Unknown;
51 }
52 
53 // These 3 helpers should probably be somewhere else
54 
55 // Look at the attached debug info to get the name of the local variable or if
56 // not known return the name of the allocainst
57 std::string inferLocalVariableName(AllocaInst *AI);
58 
59 // returns a source file + line if debug info is present valid, otherwise falls
60 // back to "<somewhere in $FUNCTION_NAME>"
61 std::string inferSourceLocation(Instruction *Inst);
62 std::string inferSourceLocation(const DebugLoc &DL, StringRef Function);
63 
64 } // end namespace cheri
65 
66 } // end namespace llvm
67 
68 #endif // LLVM_TRANSFORMS_UTILS_CHERISETBOUNDS_H
69